I'm trying to open a folder in explorer with a file selected.
我正在尝试使用选中的文件在资源管理器中打开一个文件夹。
The following code produces a file not found exception:
以下代码生成一个未找到文件的异常:
System.Diagnostics.Process.Start(
"explorer.exe /select,"
+ listView1.SelectedItems[0].SubItems[1].Text + "\\"
+ listView1.SelectedItems[0].Text);
How can I get this command to execute in C#?
如何在C#中执行此命令?
10 个解决方案
#1
60
Use this method:
使用此方法:
Process.Start(String, String)
First argument is an application (explorer.exe), second method argument are arguments of the application you run.
第一个参数是应用程序(explorer.exe),第二个方法参数是您运行的应用程序的参数。
For example:
in CMD:
explorer.exe -p
in C#:
Process.Start("explorer.exe", "-p")
#2
268
// 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);
#3
31
Just my 2 cents worth, if your filename contains spaces, ie "c:\My File Contains Spaces.txt", you'll need to surround the filename with quotes otherwise explorer will assume that the othe words are different arguments...
只需要我的2美分,如果你的文件名包含空格,即“c:\ My File Contains Spaces.txt”,你需要用引号括起文件名,否则探险家将假设其他词是不同的参数......
string argument = "/select, \"" + filePath +"\"";
#4
30
If your path contains comma's, putting quotes around the path will work when using Process.Start(ProcessStartInfo).
如果您的路径包含逗号,则在使用Process.Start(ProcessStartInfo)时,在路径周围放置引号将起作用。
It will NOT work when using Process.Start(string, string) however. It seems like Process.Start(string, string) actually removes the quotes inside of your args.
但是,在使用Process.Start(string,string)时它不起作用。似乎Process.Start(string,string)实际上删除了args中的引号。
Here is a simple example that works for me.
这是一个适合我的简单示例。
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);
#5
18
Samuel Yang answer tripped me up, here is my 3 cents worth.
塞缪尔杨的回答绊了我,这是我的3美分价值。
Adrian Hum is right, make sure you put quotes around your filename. Not because it can't handle spaces as zourtney pointed out, but because it will recognize the commas (and possibly other characters) in filenames as separate arguments. So it should look as Adrian Hum suggested.
Adrian Hum是对的,请确保在文件名周围加上引号。不是因为它不能像zourtney指出的那样处理空格,而是因为它会将文件名中的逗号(可能还有其他字符)识别为单独的参数。所以看起来应该像Adrian Hum所说的那样。
string argument = "/select, \"" + filePath +"\"";
#6
12
Use "/select,c:\file.txt"
Notice there should be a comma after /select instead of space..
注意/ select后面应该有一个逗号而不是空格。
#7
7
Using Process.Start
on explorer.exe
with the /select
argument oddly only works for paths less than 120 characters long.
在explorer.exe上使用带有/ select参数的Process.Start奇怪地仅适用于长度小于120个字符的路径。
I had to use a native windows method to get it to work in all cases:
我不得不使用本机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);
}
}
#8
5
You need to put the arguments to pass ("/select etc") in the second parameter of the Start method.
您需要在Start方法的第二个参数中传递参数(“/ select etc”)。
#9
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)
#10
2
The most possible reason for it not to find the file is the path having spaces in. For example, it won't find "explorer /select,c:\space space\space.txt".
它找不到文件的最可能原因是有空格的路径。例如,它找不到“explorer / select,c:\ space space \ space.txt”。
Just add double quotes before and after the path, like;
只需在路径前后添加双引号即可;
explorer /select,"c:\space space\space.txt"
or do the same in C# with
或者用C#做同样的事情
System.Diagnostics.Process.Start("explorer.exe","/select,\"c:\space space\space.txt\"");
#1
60
Use this method:
使用此方法:
Process.Start(String, String)
First argument is an application (explorer.exe), second method argument are arguments of the application you run.
第一个参数是应用程序(explorer.exe),第二个方法参数是您运行的应用程序的参数。
For example:
in CMD:
explorer.exe -p
in C#:
Process.Start("explorer.exe", "-p")
#2
268
// 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);
#3
31
Just my 2 cents worth, if your filename contains spaces, ie "c:\My File Contains Spaces.txt", you'll need to surround the filename with quotes otherwise explorer will assume that the othe words are different arguments...
只需要我的2美分,如果你的文件名包含空格,即“c:\ My File Contains Spaces.txt”,你需要用引号括起文件名,否则探险家将假设其他词是不同的参数......
string argument = "/select, \"" + filePath +"\"";
#4
30
If your path contains comma's, putting quotes around the path will work when using Process.Start(ProcessStartInfo).
如果您的路径包含逗号,则在使用Process.Start(ProcessStartInfo)时,在路径周围放置引号将起作用。
It will NOT work when using Process.Start(string, string) however. It seems like Process.Start(string, string) actually removes the quotes inside of your args.
但是,在使用Process.Start(string,string)时它不起作用。似乎Process.Start(string,string)实际上删除了args中的引号。
Here is a simple example that works for me.
这是一个适合我的简单示例。
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);
#5
18
Samuel Yang answer tripped me up, here is my 3 cents worth.
塞缪尔杨的回答绊了我,这是我的3美分价值。
Adrian Hum is right, make sure you put quotes around your filename. Not because it can't handle spaces as zourtney pointed out, but because it will recognize the commas (and possibly other characters) in filenames as separate arguments. So it should look as Adrian Hum suggested.
Adrian Hum是对的,请确保在文件名周围加上引号。不是因为它不能像zourtney指出的那样处理空格,而是因为它会将文件名中的逗号(可能还有其他字符)识别为单独的参数。所以看起来应该像Adrian Hum所说的那样。
string argument = "/select, \"" + filePath +"\"";
#6
12
Use "/select,c:\file.txt"
Notice there should be a comma after /select instead of space..
注意/ select后面应该有一个逗号而不是空格。
#7
7
Using Process.Start
on explorer.exe
with the /select
argument oddly only works for paths less than 120 characters long.
在explorer.exe上使用带有/ select参数的Process.Start奇怪地仅适用于长度小于120个字符的路径。
I had to use a native windows method to get it to work in all cases:
我不得不使用本机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);
}
}
#8
5
You need to put the arguments to pass ("/select etc") in the second parameter of the Start method.
您需要在Start方法的第二个参数中传递参数(“/ select etc”)。
#9
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)
#10
2
The most possible reason for it not to find the file is the path having spaces in. For example, it won't find "explorer /select,c:\space space\space.txt".
它找不到文件的最可能原因是有空格的路径。例如,它找不到“explorer / select,c:\ space space \ space.txt”。
Just add double quotes before and after the path, like;
只需在路径前后添加双引号即可;
explorer /select,"c:\space space\space.txt"
or do the same in C# with
或者用C#做同样的事情
System.Diagnostics.Process.Start("explorer.exe","/select,\"c:\space space\space.txt\"");