Answers:
// 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(ProcessStartInfo)时可以在路径两边加上引号。
但是,当使用Process.Start(string,string)时,它将不起作用。看起来Process.Start(string,string)实际上删除了args内部的引号。
这是一个对我有用的简单示例。
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);
如果我的文件名中包含空格,即“ c:\ My File Contains Spaces.txt”,则仅值2美分,您需要在文件名中加上引号,否则资源管理器会假设其他单词是不同的参数...
string argument = "/select, \"" + filePath +"\"";
塞缪尔·杨(Samuel Yang)的回答使我绊倒了,这是我3美分的价值。
Adrian Hum是正确的,请确保在文件名两边加上引号。并不是因为它不能如zourtney所指出的那样处理空格,而是因为它会将文件名中的逗号(可能还有其他字符)识别为单独的参数。因此,它应该看起来像Adrian Hum建议的那样。
string argument = "/select, \"" + filePath +"\"";
filePath
不含"
。在Windows系统上,此字符显然是非法的,但在所有其他系统(例如POSIXish系统)上都是允许的,因此,如果要移植,则需要更多代码。
将该参数与Process.Start
on explorer.exe
一起使用/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);
}
}
使用“ /select,c:\file.txt”
请注意,/ select后应有一个逗号而不是空格。
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;
}
}