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

时间:2021-11-13 13:03:16

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");

4 个解决方案

#1


247  

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

为什么不Process.Start(@“c:\测试”);?

#2


13  

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!

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

#3


9  

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网站。

#4


0  

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 dir的方法是在应用程序终止时关闭explorer窗口。

#1


247  

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

为什么不Process.Start(@“c:\测试”);?

#2


13  

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!

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

#3


9  

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网站。

#4


0  

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 dir的方法是在应用程序终止时关闭explorer窗口。