我正在尝试使用选中的文件在资源管理器中打开一个文件夹.
以下代码生成一个未找到文件的异常:
System.Diagnostics.Process.Start(
"explorer.exe /select,"
+ listView1.SelectedItems[0].SubItems[1].Text + "\\"
+ listView1.SelectedItems[0].Text);
Run Code Online (Sandbox Code Playgroud)
如何在C#中执行此命令?
小智 302
// 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);
Run Code Online (Sandbox Code Playgroud)
Tom*_*ski 56
使用此方法:
Process.Start(String, String)
Run Code Online (Sandbox Code Playgroud)
第一个参数是应用程序(explorer.exe),第二个方法参数是您运行的应用程序的参数.
例如:
在CMD:
explorer.exe -p
Run Code Online (Sandbox Code Playgroud)
在C#中:
Process.Start("explorer.exe", "-p")
Run Code Online (Sandbox Code Playgroud)
Jan*_*nen 32
如果您的路径包含逗号,则在使用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);
Run Code Online (Sandbox Code Playgroud)
小智 31
只需要我的2美分,如果你的文件名包含空格,即"c:\ My File Contains Spaces.txt",你需要用引号括住文件名,否则探险家将假设其他词是不同的参数......
string argument = "/select, \"" + filePath +"\"";
Run Code Online (Sandbox Code Playgroud)
Phi*_*ick 18
塞缪尔杨的回答绊了我,这是我3美分的价值.
Adrian Hum是对的,请确保在文件名周围加上引号.不是因为它不能像zourtney指出的那样处理空格,而是因为它会将文件名中的逗号(可能还有其他字符)识别为单独的参数.所以看起来应该像Adrian Hum所说的那样.
string argument = "/select, \"" + filePath +"\"";
Run Code Online (Sandbox Code Playgroud)
Ran*_*ngy 12
使用Process.Start上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);
}
}
Run Code Online (Sandbox Code Playgroud)
小智 9
它找不到文件的最可能原因是路径中有空格。例如,它不会找到“explorer /select,c:\space space\space.txt”。
只需在路径前后添加双引号,例如;
explorer /select,"c:\space space\space.txt"
Run Code Online (Sandbox Code Playgroud)
或在 C# 中使用
System.Diagnostics.Process.Start("explorer.exe","/select,\"c:\space space\space.txt\"");
Run Code Online (Sandbox Code Playgroud)
小智 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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
134254 次 |
| 最近记录: |