2008-12-02 94 views
113

我試圖在選定文件的情況下在資源管理器中打開一個文件夾。在資源管理器中打開一個文件夾並選擇一個文件

下面的代碼生成一個文件,未發現異常:

System.Diagnostics.Process.Start(
    "explorer.exe /select," 
    + listView1.SelectedItems[0].SubItems[1].Text + "\\" 
    + listView1.SelectedItems[0].Text); 

我怎樣才能得到這個命令在C#中執行?

回答

61

使用this method

Process.Start(String, String) 

第一個參數是一個應用程序(explorer.exe的),第二個方法的參數是運行應用程序的參數。

例如:

在CMD

在C#:

Process.Start("explorer.exe", "-p") 
+4

這不選擇該文件像塞繆爾·楊的回答 – henon 2017-06-19 05:22:24

5

您需要在Start方法的第二個參數中傳遞參數(「/ select etc」)。

11

使用 「/select,c:\file.txt」

通知應該在/選擇空格後用逗號。

233
// suppose that we have a test.txt at E:\ 
string filePath = @"E:\test.txt"; 
if (!File.Exists(filePath)) 
{ 
    return; 
} 

// combine the arguments together 
// it doesn't matter if there is a space after ',' 
string argument = "/select, \"" + filePath +"\""; 

System.Diagnostics.Process.Start("explorer.exe", argument); 
+1

這對我來說是顯著:)它不僅打開了目錄,但選擇的特定文件以及:)感謝問候 – 2011-07-28 11:58:26

+1

它的作品就像一個魅力,但任何想法我們怎麼能做到這一點的多個文件? – Pankaj 2012-12-26 22:17:54

+5

小記,如果我的文件路徑使用正斜槓,文件路徑的/ select參數似乎不適用於我。因此,我必須做filePath = filePath.Replace('/','\\'); – 2013-01-29 18:48:44

30

只需要我的2美分價值,如果您的文件名包含空格,即「c:\我的文件包含Spaces.txt」,您需要用引號包圍文件名,否則資源管理器會假設其他字不同論點...

string argument = "/select, \"" + filePath +"\""; 
17

Samuel Yang回答絆倒了我,這是我的3美分價值。

阿德里安嗡嗡聲是正確的,確保你把你的文件名引號。不是因爲它不能像zourtney指出的那樣處理空格,而是因爲它會將文件名中的逗號(可能還有其他字符)識別爲單獨的參數。所以它應該看起來像Adrian Hum建議的那樣。

string argument = "/select, \"" + filePath +"\""; 
5
string windir = Environment.GetEnvironmentVariable("windir"); 
if (string.IsNullOrEmpty(windir.Trim())) { 
    windir = "C:\\Windows\\"; 
} 
if (!windir.EndsWith("\\")) { 
    windir += "\\"; 
}  

FileInfo fileToLocate = null; 
fileToLocate = new FileInfo("C:\\Temp\\myfile.txt"); 

ProcessStartInfo pi = new ProcessStartInfo(windir + "explorer.exe"); 
pi.Arguments = "/select, \"" + fileToLocate.FullName + "\""; 
pi.WindowStyle = ProcessWindowStyle.Normal; 
pi.WorkingDirectory = windir; 

//Start Process 
Process.Start(pi) 
24

如果路徑中包含逗號,把路徑引號包圍使用的Process.Start(的ProcessStartInfo)時會工作。

但是,使用Process.Start(字符串,字符串)時不起作用。看起來像Process.Start(string,string)實際上刪除了你的參數裏面的引號。

這是一個簡單的例子,適合我。

string p = @"C:\tmp\this path contains spaces, and,commas\target.txt"; 
string args = string.Format("/e, /select, \"{0}\"", p); 

ProcessStartInfo info = new ProcessStartInfo(); 
info.FileName = "explorer"; 
info.Arguments = args; 
Process.Start(info); 
3

使用上explorer.exeProcess.Start/select奇怪的說法僅適用於小於長120個字符的路徑。

我不得不使用本機Windows方法,讓它在所有情況下工作。

[DllImport("shell32.dll", SetLastError = true)] 
public static extern int SHOpenFolderAndSelectItems(IntPtr pidlFolder, uint cidl, [In, MarshalAs(UnmanagedType.LPArray)] IntPtr[] apidl, uint dwFlags); 

[DllImport("shell32.dll", SetLastError = true)] 
public static extern void SHParseDisplayName([MarshalAs(UnmanagedType.LPWStr)] string name, IntPtr bindingContext, [Out] out IntPtr pidl, uint sfgaoIn, [Out] out uint psfgaoOut); 

public static void OpenFolderAndSelectItem(string folderPath, string file) 
{ 
    IntPtr nativeFolder; 
    uint psfgaoOut; 
    SHParseDisplayName(folderPath, IntPtr.Zero, out nativeFolder, 0, out psfgaoOut); 

    if (nativeFolder == IntPtr.Zero) 
    { 
     // Log error, can't find folder 
     return; 
    } 

    IntPtr nativeFile; 
    SHParseDisplayName(Path.Combine(folderPath, file), IntPtr.Zero, out nativeFile, 0, out psfgaoOut); 

    IntPtr[] fileArray; 
    if (nativeFile == IntPtr.Zero) 
    { 
     // Open the folder without the file selected if we can't find the file 
     fileArray = new IntPtr[0]; 
    } 
    else 
    { 
     fileArray = new IntPtr[] { nativeFile }; 
    } 

    SHOpenFolderAndSelectItems(nativeFolder, (uint)fileArray.Length, fileArray, 0); 

    Marshal.FreeCoTaskMem(nativeFolder); 
    if (nativeFile != IntPtr.Zero) 
    { 
     Marshal.FreeCoTaskMem(nativeFile); 
    } 
} 
2

的最可能的原因是沒有找到該文件是具有在空間路徑,例如,它將找不到「explorer/select,c:\ space space \ space.txt」。

只需在路徑前後添加雙引號,如;

explorer /select,"c:\space space\space.txt" 

或做在C#中同樣有

System.Diagnostics.Process.Start("explorer.exe","/select,\"c:\space space\space.txt\""); 
相關問題