在Windows資源管理器中搜索文件並右鍵單擊搜索結果中的文件;有一個選項:「打開文件位置」。我想在我的C#WinForm中實現相同的功能。我這樣做:打開文件位置
if (File.Exists(filePath)
{
openFileDialog1.InitialDirectory = new FileInfo(filePath).DirectoryName;
openFileDialog1.ShowDialog();
}
有沒有更好的方法來做到這一點?
在Windows資源管理器中搜索文件並右鍵單擊搜索結果中的文件;有一個選項:「打開文件位置」。我想在我的C#WinForm中實現相同的功能。我這樣做:打開文件位置
if (File.Exists(filePath)
{
openFileDialog1.InitialDirectory = new FileInfo(filePath).DirectoryName;
openFileDialog1.ShowDialog();
}
有沒有更好的方法來做到這一點?
如果openFileDialog_View
是OpenFileDialog那麼你只會得到一個對話框提示用戶打開一個文件。我假設你想實際上打開在資源管理器中的位置。
你可以這樣做:
if (File.Exists(filePath))
{
Process.Start("explorer.exe", filePath);
}
要選擇文件explorer.exe
需要/select
的說法是這樣的:
explorer.exe /select, <filelist>
我得到這個從SO後:Opening a folder in explorer and selecting a file
所以你的代碼將是:
if (File.Exists(filePath))
{
Process.Start("explorer.exe", "/select, " + filePath);
}
這應該是「explorer.exe」 – scartag 2012-03-10 11:46:04
很好先生gideon。但我希望該文件被選中,如何? – 2012-03-10 12:14:36
@H_wardak更新了我的答案。一個簡單的[谷歌搜索](http://www.google.co.in/webhp?sourceid=chrome-instant&ix=sea&ie=UTF-8&ion=1#hl=en&output=search&sclient=psy-ab&q=open%20explorer%20and %20select%20file&OQ =&水溶液=&AQI =&AQL =&gs_sm =&gs_upl =&gs_l =&PBX = 1&FP = 237055d012d02b32&IX =海&離子= 1&BAV = on.2,or.r_gc.r_pw.r_cp.r_qf。,cf.osb&BIW = 1366&波黑= 643)把我帶到那個SO帖子。 – gideon 2012-03-10 12:38:10
這是我如何在我的代碼中做到這一點。這將在資源管理器中打開文件目錄,並按照Windows資源管理器的方式選擇指定的文件。
if (File.Exists(path))
{
Process.Start(new ProcessStartInfo("explorer.exe", " /select, " + path);
}
如果我使用「ProcessStartInfo」有什麼好處?它也是沒有它的工作。 – 2012-03-11 06:20:06
您的解決方案面臨的問題是什麼?如果'openFileDialog_View'是一個OpenFileDialog,那麼你會得到一個對話框,提示用戶**打開一個文件。 – gideon 2012-03-10 11:40:13
我想要其他更好的方法嗎? – 2012-03-10 11:42:01
請參閱我的回答然後:) – gideon 2012-03-10 11:43:05