C# 在资源管理器中打开文件夹并选择文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/334630/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Opening a folder in explorer and selecting a file
提问by Michael L
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# 中执行?
采纳答案by Tomasz Smykowski
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:
在 CMD 中:
explorer.exe -p
in C#:
在 C# 中:
Process.Start("explorer.exe", "-p")
回答by Paul
You need to put the arguments to pass ("/select etc") in the second parameter of the Start method.
您需要将要传递的参数(“/select 等”)放在 Start 方法的第二个参数中。
回答by Tomasz Smykowski
Use "/select,c:\file.txt"
使用“/select,c:\file.txt”
Notice there should be a comma after /select instead of space..
注意 /select 后面应该有一个逗号而不是空格..
回答by Samuel Yang
// 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);
回答by Adrian Hum
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 +"\"";
回答by Phillip Hustwick
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 +"\"";
回答by Corey
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)
回答by Jan Croonen
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);
回答by RandomEngy
Using Process.Start
on explorer.exe
with the /select
argument oddly only works for paths less than 120 characters long.
奇怪的是,将Process.Start
onexplorer.exe
与/select
参数一起使用仅适用于长度小于 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);
}
}
回答by Zztri
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\"");