168

我正在尝试在资源管理器中打开一个文件夹并选择一个文件。

以下代码产生一个文件未找到异常:

System.Diagnostics.Process.Start(
    "explorer.exe /select," 
    + listView1.SelectedItems[0].SubItems[1].Text + "\\" 
    + listView1.SelectedItems[0].Text);

如何让这个命令在 C# 中执行?

4

11 回答 11

370
// 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);
于 2009-03-30T06:09:57.400 回答
47

使用此方法

Process.Start(String, String)

第一个参数是应用程序(explorer.exe),第二个方法参数是您运行的应用程序的参数。

例如:

在 CMD 中:

explorer.exe -p

在 C# 中:

Process.Start("explorer.exe", "-p")
于 2008-12-02T16:46:29.723 回答
43

如果您的路径包含逗号,则在使用 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);
于 2012-03-28T09:38:45.323 回答
31

只是我的 2 美分,如果你的文件名包含空格,即“c:\My File Contains Spaces.txt”,你需要用引号将文件名括起来,否则资源管理器会认为其他词是不同的参数......

string argument = "/select, \"" + filePath +"\"";
于 2010-10-03T23:05:36.990 回答
21

奇怪的是,与参数一起使用Process.Starton仅适用于长度小于 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);
    }
}
于 2016-09-10T15:08:39.387 回答
18

Samuel Yang 的回答让我大吃一惊,这是我的 3 美分。

Adrian Hum 是对的,请确保在文件名周围加上引号。不是因为它不能像 zourtney 指出的那样处理空格,而是因为它将文件名中的逗号(可能还有其他字符)识别为单独的参数。所以它应该看起来像 Adrian Hum 建议的那样。

string argument = "/select, \"" + filePath +"\"";
于 2012-01-30T05:41:29.587 回答
14

使用“/select,c:\file.txt”

注意 /select 后面应该有一个逗号而不是空格。

于 2009-02-13T06:28:52.560 回答
9

它找不到文件的最可能原因是路径中有空格。例如,它不会找到“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\"");
于 2017-11-08T14:55:03.427 回答
6

您需要将要传递的参数(“/select 等”)放在 Start 方法的第二个参数中。

于 2008-12-02T16:46:25.577 回答
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)
于 2012-03-05T17:21:18.487 回答
2

这可能有点矫枉过正,但我​​喜欢便利功能,所以拿这个:

    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;
    }
}
于 2019-04-23T17:45:01.237 回答