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

前端 未结 4 535
执笔经年
执笔经年 2020-12-02 08:11

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?

I would expect something like this

相关标签:
4条回答
  • 2020-12-02 08:33

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

    0 讨论(0)
  • 2020-12-02 08:40
    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.

    0 讨论(0)
  • 2020-12-02 08:54

    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!

    0 讨论(0)
  • 2020-12-02 08:55

    You can use 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.

    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.

    0 讨论(0)
提交回复
热议问题