Coder Perfect

How can I use a WPF program to open Windows Explorer to a certain directory?


How do I open the Windows explorer to a certain directory when a user clicks on a button in a WPF application?

Something along these lines, for example:


Asked by Edward Tanguay

Solution #1

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

Answered by Jamie Penney

Solution #2

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

The second option of merely specifying the tgt dir would close the explorer window when my application closed, therefore I had to use this.

Answered by MarkyMarksFunkyBunch

Solution #3

This should work:

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

Alternatively, if you want to execute programs/open files and/or directories, you can use the following method:

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

    StartInformation.FileName = path;

    Process process = Process.Start(StartInformation);

    process.EnableRaisingEvents = true;

Then call the method, specifying either the file and/or folder location or the application’s name in the parenthesis. I hope this information was useful.

Answered by Anthony Smyth

Solution #4

System.Diagnostics.Process.Start can be used.

Alternatively, you can utilize the WinApi directly to run explorer.exe with something like the following. ShellExecute’s fourth option can be used to provide a starting directory.

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

    public enum ShowCommands : int
        SW_HIDE = 0,
        SW_SHOWNORMAL = 1,
        SW_NORMAL = 1,
        SW_MAXIMIZE = 3,
        SW_SHOW = 5,
        SW_MINIMIZE = 6,
        SW_SHOWNA = 8,
        SW_RESTORE = 9,
        SW_SHOWDEFAULT = 10,
        SW_FORCEMINIMIZE = 11,
        SW_MAX = 11

    static extern IntPtr ShellExecute(
        IntPtr hwnd,
        string lpOperation,
        string lpFile,
        string lpParameters,
        string lpDirectory,
        ShowCommands nShowCmd);

The declarations came from the website

Answered by Abel

Solution #5

Here’s what I found to be effective in my situation:

Basically, call “start C:/path” from the command line. After that, type “start c:/path && exit” to exit the terminal.


        public static void WindowsExplorerOpen(string path)
            CommandLine(path, $"start {path}");

        private static void CommandLine(string workingDirectory, string Command)
            ProcessStartInfo ProcessInfo;
            Process Process;

            ProcessInfo = new ProcessStartInfo("cmd.exe", "/K " + Command + " && exit");
            ProcessInfo.WorkingDirectory = workingDirectory;
            ProcessInfo.CreateNoWindow = true;
            ProcessInfo.UseShellExecute = true;
            ProcessInfo.WindowStyle = ProcessWindowStyle.Hidden;

            Process = Process.Start(ProcessInfo);

I couldn’t get either of them to work for me:

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

Answered by Jesus is Lord

Post is based on