在资源管理器中打开文件夹并选择文件

Mic*_*l L 142 c# explorer

我正在尝试使用选中的文件在资源管理器中打开一个文件夹.

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

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)

  • 小注意,如果我的文件路径使用正斜杠,带文件路径的/ select参数似乎对我不起作用.因此我必须做filePath = filePath.Replace('/','\\'); (6认同)
  • 如其他地方所述,您的路径应包含在引号中 - 这可以防止包含逗号的目录或文件名出现问题. (6认同)
  • 我正在争论这个问题有时上面的方法不起作用,因为该文件包含一个逗号.如果我读过Kaganar的评论,那将会节省我一个小时的工作量.我敦促Samuel Yang将上面的代码修改为:string argument = @"/ select"+"\""+ filePath +"\"" (4认同)
  • 这对我来说很重要:)它不仅打开了目录,还选择了特定的文件:)谢谢问候 (2认同)
  • 它就像一个魅力,但任何想法我们如何为多个文件做到这一点? (2认同)

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)

  • 这不会像Samuel Yangs那样选择文件 (20认同)
  • -p 不足以选择文件 (4认同)

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)

  • 这是正确的答案,接受的答案不起作用,杨的答案也不起作用。 (2认同)

小智 31

只需要我的2美分,如果你的文件名包含空格,即"c:\ My File Contains Spaces.txt",你需要用引号括住文件名,否则探险家将假设其他词是不同的参数......

string argument = "/select, \"" + filePath +"\"";
Run Code Online (Sandbox Code Playgroud)

  • 请阅读下面关于Phil Hustwick的回答,说明为什么你应该引用引号 (8认同)
  • 实际上,不,你没有.@Samuel Yang的例子适用于带空格的路径(经过测试的Win7) (3认同)

Phi*_*ick 18

塞缪尔杨的回答绊了我,这是我3美分的价值.

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

string argument = "/select, \"" + filePath +"\"";
Run Code Online (Sandbox Code Playgroud)


小智 13

使用"/select,c:\file.txt"

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


Ran*_*ngy 12

使用Process.Startexplorer.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)

  • 这就是它应该如何完成,我还将为 sfgao 创建一个标志,并传递该枚举而不是 uint (2认同)

小智 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)


Pau*_*aul 6

您需要将参数传递给Start方法的第二个参数("/ select etc").


小智 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)