C# 打開指定目錄並定位到文件


1.

打開文件夾:

System.Diagnostics.Process.Start(FilePath);

 

打開文件夾中某個文件:

System.Diagnostics.Process.Start(FilePath+"/"+FileName);

 

 

打開文件夾並選中單個文件:

System.Diagnostics.Process.Start("Explorer", "/select,"+ FilePath+"\\"+FileName);

 

System.Diagnostics.Process.Start("Explorer.exe", "/select,"+ FilePath+"\\"+FileName);

 

用IE打開文件:

System.Diagnostics.Process.Start("Explorer",FilePath+"\\"+FileName);

 

System.Diagnostics.Process.Start("Explorer.exe",FilePath+"\\"+FileName);

 

注:(explorer,explorer.exe,select,不區分大小寫,”/selecet,”其中”/,”都不能少,FilePath為文件路徑不包含文件名)

擴展提示:可以采用 Application.StartupPath 獲得應用程序所在的目錄。

 

2.

 

C# 實現:

  1.  1  
     2 private void OpenFolderAndSelectFile(String fileFullName)
     3  
     4 {
     5  
     6 System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("Explorer.exe");
     7  
     8 psi.Arguments = "/e,/select," + fileFullName;
     9  
    10 System.Diagnostics.Process.Start(psi);
    11  
    12 }

     

復制代碼

 

參考:

Windows 資源管理器參數的用法。 

Explorer /n
此命令使用默認設置打開一個資源管理器窗口。顯示的內容通常是安裝 Windows 的驅動器的根目錄。
Explorer /e
此命令使用默認視圖啟動 Windows 資源管理器。
Explorer /e,C:\Windows
此命令使用默認視圖啟動 Windows 資源管理器,並把焦點定位在 C:\Windows。
Explorer /root, C:\Windows\Cursors
此命令啟動 Windows 資源管理器后焦點定位在 C:\Windows\Cursors folder。此示例使用 C:\Windows\Cursors 作為 Windows 資源管理器的“根”目錄。
 

備注:請注意命令中“/root”參數后面的逗號。

Explorer /select, C:\Windows\Cursors\banana.ani
此命令啟動 Windows 資源管理器后選定“C:\Windows\Cursors\banana.ani”文件。

備注:請注意命令中“/select”參數后面的逗號。
Windows 資源管理器參數可以在一個命令中進行組合。以下示例顯示了 Windows 資源管理器命令行參數的組合。
Explorer /root, \\server\share, select, Program.exe
此命令啟動 Windows 資源管理器時以遠程共享作為“根”文件夾,而且 Program.exe 文件將被選中。
回到頂端
更改 Windows 資源管理器默認啟動文件夾
若要更改 Windows 資源管理器的默認啟動文件夾,請:
單擊開始,指向所有程序,指向附件,然后右鍵單擊Windows Explorer。
在出現的菜單上,單擊屬性。
在“目標”框中,將“/root”命令行參數附加到“%SystemRoot%\Explorer.exe”命令之后,並使用您希望的啟動位置。例如,如果您希望 Windows 資源管理器啟動后定位在 C 驅動器的根,則請將該命令編輯為:
%SystemRoot%\Explorer.exe /root, C:\


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM