我正在尝试在资源管理器中打开一个文件夹并选择一个文件。
以下代码产生一个文件未找到异常:
System.Diagnostics.Process.Start(
"explorer.exe /select,"
+ listView1.SelectedItems[0].SubItems[1].Text + "\\"
+ listView1.SelectedItems[0].Text);
如何让这个命令在 C# 中执行?
// 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);
使用此方法:
Process.Start(String, String)
第一个参数是应用程序(explorer.exe),第二个方法参数是您运行的应用程序的参数。
例如:
在 CMD 中:
explorer.exe -p
在 C# 中:
Process.Start("explorer.exe", "-p")
如果您的路径包含逗号,则在使用 Process.Start(ProcessStartInfo) 时,在路径周围加上引号将起作用。
但是,在使用 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);
只是我的 2 美分,如果你的文件名包含空格,即“c:\My File Contains Spaces.txt”,你需要用引号将文件名括起来,否则资源管理器会认为其他词是不同的参数......
string argument = "/select, \"" + filePath +"\"";
奇怪的是,与参数一起使用Process.Start
on仅适用于长度小于 120 个字符的路径。explorer.exe
/select
我必须使用本机 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);
}
}
Samuel Yang 的回答让我大吃一惊,这是我的 3 美分。
Adrian Hum 是对的,请确保在文件名周围加上引号。不是因为它不能像 zourtney 指出的那样处理空格,而是因为它将文件名中的逗号(可能还有其他字符)识别为单独的参数。所以它应该看起来像 Adrian Hum 建议的那样。
string argument = "/select, \"" + filePath +"\"";
使用“/select,c:\file.txt”
注意 /select 后面应该有一个逗号而不是空格。
它找不到文件的最可能原因是路径中有空格。例如,它不会找到“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\"");
您需要将要传递的参数(“/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)
这可能有点矫枉过正,但我喜欢便利功能,所以拿这个:
public static void ShowFileInExplorer(FileInfo file) {
StartProcess("explorer.exe", null, "/select, "+file.FullName.Quote());
}
public static Process StartProcess(FileInfo file, params string[] args) => StartProcess(file.FullName, file.DirectoryName, args);
public static Process StartProcess(string file, string workDir = null, params string[] args) {
ProcessStartInfo proc = new ProcessStartInfo();
proc.FileName = file;
proc.Arguments = string.Join(" ", args);
Logger.Debug(proc.FileName, proc.Arguments); // Replace with your logging function
if (workDir != null) {
proc.WorkingDirectory = workDir;
Logger.Debug("WorkingDirectory:", proc.WorkingDirectory); // Replace with your logging function
}
return Process.Start(proc);
}
这是我用作 <string>.Quote() 的扩展函数:
static class Extensions
{
public static string Quote(this string text)
{
return SurroundWith(text, "\"");
}
public static string SurroundWith(this string text, string surrounds)
{
return surrounds + text + surrounds;
}
}