C# 如何从 WPF 应用程序中将 Windows 资源管理器打开到某个目录?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1746079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 20:26:02  来源:igfitidea点击:

How can I open Windows Explorer to a certain directory from within a WPF app?

c#wpfwindows-explorer

提问by Edward Tanguay

In a WPF application, when a user clicks on a button I want to open the Windows explorer to a certain directory, how do I do that?

在 WPF 应用程序中,当用户单击按钮时,我想将 Windows 资源管理器打开到某个目录,我该怎么做?

I would expect something like this:

我希望是这样的:

Windows.OpenExplorer("c:\test");

采纳答案by Jamie Penney

Why not Process.Start(@"c:\test");?

为什么不Process.Start(@"c:\test");呢?

回答by Abel

You can use System.Diagnostics.Process.Start.

您可以使用System.Diagnostics.Process.Start.

Or use the WinApi directly with something like the following, which will launch explorer.exe. You can use the fourth parameter to ShellExecute to give it a starting directory.

或者直接使用 WinApi 和下面类似的东西,这将启动 explorer.exe。您可以使用 ShellExecute 的第四个参数为其指定一个起始目录。

public partial class Window1 : Window
{
    public Window1()
    {
        ShellExecute(IntPtr.Zero, "open", "explorer.exe", "", "", ShowCommands.SW_NORMAL);
        InitializeComponent();
    }

    public enum ShowCommands : int
    {
        SW_HIDE = 0,
        SW_SHOWNORMAL = 1,
        SW_NORMAL = 1,
        SW_SHOWMINIMIZED = 2,
        SW_SHOWMAXIMIZED = 3,
        SW_MAXIMIZE = 3,
        SW_SHOWNOACTIVATE = 4,
        SW_SHOW = 5,
        SW_MINIMIZE = 6,
        SW_SHOWMINNOACTIVE = 7,
        SW_SHOWNA = 8,
        SW_RESTORE = 9,
        SW_SHOWDEFAULT = 10,
        SW_FORCEMINIMIZE = 11,
        SW_MAX = 11
    }

    [DllImport("shell32.dll")]
    static extern IntPtr ShellExecute(
        IntPtr hwnd,
        string lpOperation,
        string lpFile,
        string lpParameters,
        string lpDirectory,
        ShowCommands nShowCmd);
}

The declarations come from the pinvoke.net website.

声明来自pinvoke.net 网站

回答by Anthony Smyth

This should work:

这应该有效:

Process.Start(@"<directory goes here>")

Or if you'd like a method to run programs/open files and/or folders:

或者,如果您想要一种运行程序/打开文件和/或文件夹的方法:

private void StartProcess(string path)
{
    ProcessStartInfo StartInformation = new ProcessStartInfo();

    StartInformation.FileName = path;

    Process process = Process.Start(StartInformation);

    process.EnableRaisingEvents = true;
}

And then call the method and in the parenthesis put either the directory of the file and/or folder there or the name of the application. Hope this helped!

然后调用该方法并在括号中放置文件和/或文件夹的目录或应用程序的名称。希望这有帮助!

回答by MarkyMarksFunkyBunch

Process.Start("explorer.exe" , @"C:\Users");

I had to use this, the other way of just specifying the tgt dir would shut the explorer window when my application terminated.

我不得不使用它,另一种仅指定 tgt 目录的方法会在我的应用程序终止时关闭资源管理器窗口。