197

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:

Windows.OpenExplorer("c:\test");
Edward Tanguay
  • 189,012
  • 314
  • 712
  • 1,047

5 Answers5

355

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

Jamie Penney
  • 9,424
  • 3
  • 29
  • 37
  • 10
    Note: You can use this to run other applications as well. `Process.Start("calc.exe");` will run Calculator. You can pass it the full path to an executable and it will run it. – Jamie Penney Nov 17 '09 at 02:33
  • 1
    LOL, yes, why not. Funny, had Application.Run in my head, couldn't get to the ubiquitous Process.Start and thought WPF was playing games with me. – Abel Nov 17 '09 at 03:56
  • This helped me as I was trying to load explorer and add the folder as a working directory using process.start. Thanks for the answer. – Michael Eakins Sep 28 '12 at 12:45
  • 16
    note: It will throw an exception if it's not there. Try Process.Start("explorer", @"c:\test"); instead, if you don't want to handle the exception. It will open a default window. Often it will be better to handle the exception, however. – amalgamate Sep 16 '13 at 17:19
  • 13
    Beware that if someone malicious (or just unaware) can get any string there, they'll be able to execute any program. @amalgamate suggestion is more secure, otherwise check if the path is a directory and if it exists before. – Christian Rondeau Sep 02 '14 at 02:42
  • Because it doesn't work. That just runs a background process on the intranet server. We want an actual window to pop up "for the user" on their machine to prevent them from having to navigate all the time. – VoidKing Sep 05 '17 at 16:48
  • 4
    Another difference between `Start(dir)` and `Start("explorer.exe", dir)` is that the former will be smart enough to focus the existing window for `dir` if there is one, while the latter opens a new window every time. – dlf May 23 '18 at 18:18
  • 1
    Does not work. I get "Access denied" exception. The answer below Process.Start("explorer.exe" , @"C:\Users"); works perfectly. – AH. Oct 22 '20 at 11:49
63
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.

MarkyMarksFunkyBunch
  • 1,060
  • 10
  • 10
  • 13
    The only answer I don't get `Access denied` exception. – Matas Vaitkevicius May 04 '20 at 12:06
  • 4
    This is working perfectly without the "Access denied" exception. Thanks. – AH. Oct 22 '20 at 11:48
  • 3
    use `Process.Start(new ProcessStartInfo(your_path) { UseShellExecute = true })` if you wan't your code to stay cross plateform. It will use your machine default shell instead of trying to find the explorer.exe which doesn't exists and unix based OS. – Erhode Sep 05 '22 at 12:18
  • @Erhode Doesn't `UseShellExecute` default to `true` anyway? – Nyerguds Sep 29 '22 at 14:50
  • 1
    @Nyerguds According to https://learn.microsoft.com/en-gb/dotnet/api/system.diagnostics.processstartinfo.useshellexecute?view=net-6.0 The value is set to true by default for .NET Framework apps and false for .NET Core apps. I had to add it in my last .NET 6 project. – Erhode Oct 04 '22 at 21:09
17

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!

Twenty
  • 5,234
  • 4
  • 32
  • 67
Anthony Smyth
  • 305
  • 4
  • 14
  • Opening a folder. Getting error on line `process.EnableRaisingEvents = true;`, because process is null. – Starwave Jan 18 '21 at 17:30
  • @Starwave Make the Process object first, set its `EnableRaisingEvents` property, and only then `Start` it? – Nyerguds Sep 29 '22 at 14:51
13

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.

Abel
  • 56,041
  • 24
  • 146
  • 247
  • 1
    yes, i was getting errors chasing that, didn't know about strike btw cool – Edward Tanguay Nov 17 '09 at 01:53
  • 1
    which, unfortunately, only works in answers / questions, but not in comments ;-). I just updated. – Abel Nov 17 '09 at 02:00
  • +1 I'll use this code to launch other apps, but the Process.Start() was exactly what I needed. – Edward Tanguay Nov 17 '09 at 02:09
  • that's what happens when you try to answer q. a 3AM: you miss the obvious ;-). Anyway, I remember I often use ShellExecute when Process.Start does not what I want (there are few scenarios that it can't handle). – Abel Nov 17 '09 at 03:55
  • what you (within the first two sentences), said would work from a winforms application, Application.Run("explorer.exe") doesn't work. Because application.run doesn't even take a string – barlop May 06 '16 at 11:45
  • 1
    due to the use of unmanaged Resources should I wrap this use inside another Class implementing `IDisposable`? – LuckyLikey Jun 27 '17 at 08:45
  • 1
    @LuckyLikey, yes, that's usually a good idea and a good design practice. – Abel Jun 28 '17 at 14:26
0

Here's what worked for me:

Basically use the command line to call "start C:/path" And exit the terminal afterward, so "start c:/path && exit"

WindowsExplorerOpen(@"C:/path");

        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);
            Process.WaitForExit();
        }

Neither of these worked for me:

Process.Start(@"c:\test");
Process.Start("explorer.exe" , @"C:\Users");
Jesus is Lord
  • 14,971
  • 11
  • 66
  • 97