在资源pipe理器中打开文件夹并select一个文件

我试图打开一个文件夹在浏览器中select一个文件。

下面的代码产生一个文件未findexception:

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

我怎样才能得到这个命令在C#中执行?

使用这个方法 :

 Process.Start(String, String) 

第一个参数是一个应用程序(explorer.exe),第二个参数是你运行的应用程序的参数。

例如:

在CMD中:

 explorer.exe -p 

在C#中:

 Process.Start("explorer.exe", "-p") 
 // 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); 

只是我的2美分的价值,如果你的文件名包含空格,即“C:\我的文件包含Spaces.txt”,你需要用引号包围文件名否则资源pipe理器将假设其他的话是不同的论点…

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

如果您的path包含逗号,则在使用Process.Start(ProcessStartInfo)时,将引号放在path的周围。

但是,使用Process.Start(string,string)时不起作用。 看起来像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); 

Samuel Yang回答我绊倒了我,这是我的3美分值。

阿德里安嗡嗡声是正确的,确保你把你的文件名引号。 不是因为它不能像zourtney指出的那样处理空格,而是因为它会将文件名中的逗号(也可能是其他字符)识别为单独的参数。 所以它应该看起来像Adrian Humbuild议的那样。

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

使用“/select,c:\file.txt”

注意,在select空格之后应该有一个逗号。

您需要将parameter passing(“/ select等”)在Start方法的第二个参数中。

 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) 

explorer.exe使用Process.Start/select参数奇怪地只适用于长度小于120个字符的path。

我不得不使用本地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); } } 

找不到文件的最可能的原因是有空格的path。例如,它不会find“explorer / select,c:\ space space \ space.txt”。

只需在path前后添加双引号,如:

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

或者在C#中执行相同的操作

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