C#實(shí)現(xiàn)打開指定目錄和指定文件的示例代碼
一、實(shí)現(xiàn)內(nèi)容
1.1實(shí)現(xiàn)的功能
想要實(shí)現(xiàn):
①打開指定的目錄;
②打開指定的目錄且選中指定文件;
③打開指定文件
1.2實(shí)現(xiàn)的效果

二、實(shí)現(xiàn)操作
/// <summary>
/// 打開目錄
/// </summary>
/// <param name="folderPath">目錄路徑(比如:C:\Users\Administrator\)</param>
private static void OpenFolder(string folderPath)
{
if (string.IsNullOrEmpty(folderPath)) return;
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
psi.Arguments = folderPath;
process.StartInfo = psi;
try
{
process.Start();
}
catch (Exception ex)
{
throw ex;
}
finally
{
process?.Close();
}
}
/// <summary>
/// 打開目錄且選中文件
/// </summary>
/// <param name="filePathAndName">文件的路徑和名稱(比如:C:\Users\Administrator\test.txt)</param>
private static void OpenFolderAndSelectedFile(string filePathAndName)
{
if (string.IsNullOrEmpty(filePathAndName)) return;
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo("Explorer.exe");
psi.Arguments = "/e,/select,"+filePathAndName;
process.StartInfo = psi;
//process.StartInfo.UseShellExecute = true;
try
{
process.Start();
}
catch (Exception ex)
{
throw ex;
}
finally
{
process?.Close();
}
}
/// <summary>
/// 打開文件
/// </summary>
/// <param name="filePathAndName">文件的路徑和名稱(比如:C:\Users\Administrator\test.txt)</param>
/// <param name="isWaitFileClose">是否等待文件關(guān)閉(true:表示等待)</param>
private static void OpenFile(string filePathAndName,bool isWaitFileClose=true)
{
Process process = new Process();
ProcessStartInfo psi = new ProcessStartInfo(filePathAndName);
process.StartInfo = psi;
process.StartInfo.UseShellExecute = true;
try
{
process.Start();
//等待打開的程序關(guān)閉
if (isWaitFileClose)
{
process.WaitForExit();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
process?.Close();
}
}三、Windows 資源管理器參數(shù)說(shuō)明
Windows資源管理器參數(shù)的說(shuō)明
| 序號(hào) | 參數(shù)命令 | 說(shuō)明 |
| 1 | Explorer /n | 此命令使用默認(rèn)設(shè)置打開一個(gè)資源管理器窗口。顯示的內(nèi)容通常是安裝 Windows 的驅(qū)動(dòng)器的根目錄 |
| 2 | Explorer /e | 此命令使用默認(rèn)視圖啟動(dòng) Windows 資源管理器 |
| 3 | Explorer /e,C:\Windows | 此命令使用默認(rèn)視圖啟動(dòng) Windows 資源管理器,并把焦點(diǎn)定位在 C:\Windows路徑上 |
| 4 | Explorer /root, C:\Windows\Cursors | 此命令啟動(dòng) Windows 資源管理器后焦點(diǎn)定位在 C:\Windows\Cursors folder路徑上。此示例使用 C:\Windows\Cursors 作為 Windows 資源管理器的“根”目錄 |
| 5 | Explorer /select, C:\Windows\Cursors\banana.ani | 此命令啟動(dòng) Windows 資源管理器后選定“C:\Windows\Cursors\banana.ani”文件。 |
| 6 | Explorer /root, \\server\share, select, Program.exe | 此命令啟動(dòng) Windows 資源管理器時(shí)以遠(yuǎn)程共享作為“根”文件夾,而且 Program.exe 文件將被選中 |
以上就是C#實(shí)現(xiàn)打開指定目錄和指定文件的示例代碼的詳細(xì)內(nèi)容,更多關(guān)于C#打開指定目錄 文件的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于WPF實(shí)現(xiàn)路徑圖標(biāo)控件
這篇文章主要介紹了如何利用WPF實(shí)現(xiàn)路徑圖標(biāo)控件,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)或工作有一定幫助,需要的小伙伴可以參考一下2023-07-07
.net2.0+ Winform項(xiàng)目實(shí)現(xiàn)彈出容器層
在實(shí)際工作中,如果能像菜單一樣彈出自定義內(nèi)容,會(huì)方便很多,比如查詢時(shí),比如下拉列表顯示多列信息時(shí),比如在填寫某個(gè)信息需要查看一些信息樹時(shí)。這個(gè)時(shí)候自定義彈出界面就顯的非常重要了2015-08-08
C# 實(shí)現(xiàn)ADSL自動(dòng)斷網(wǎng)和撥號(hào)的方法(適用于撥號(hào)用戶)
下面小編就為大家?guī)?lái)一篇C# 實(shí)現(xiàn)ADSL自動(dòng)斷網(wǎng)和撥號(hào)的方法(適用于撥號(hào)用戶)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
.NET企業(yè)級(jí)項(xiàng)目中遇到的國(guó)際化問(wèn)題和解決方法
這篇文章主要介紹了.NET企業(yè)級(jí)項(xiàng)目中遇到的國(guó)際化問(wèn)題和解決方法,說(shuō)明了理國(guó)際化問(wèn)題的一些典型例子和經(jīng)驗(yàn)之談,需要的朋友可以參考下2014-07-07

