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

5

199

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");
Glenine answered 17/11, 2009 at 1:44 Comment(0)
U
358

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

Unboned answered 17/11, 2009 at 2:1 Comment(8)
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.Unboned
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.Flatulent
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.Morsel
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.Sphalerite
Beware that if someone malicious (or just unaware) can get any string there, they'll be able to execute any program. @Sphalerite suggestion is more secure, otherwise check if the path is a directory and if it exists before.Phototransistor
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.Kerenkeresan
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.Demagogy
Does not work. I get "Access denied" exception. The answer below Process.Start("explorer.exe" , @"C:\Users"); works perfectly.Goosy
W
67
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.

Watchful answered 5/6, 2018 at 14:1 Comment(5)
The only answer I don't get Access denied exception.Tolbert
This is working perfectly without the "Access denied" exception. Thanks.Goosy
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.Timberlake
@Timberlake Doesn't UseShellExecute default to true anyway?Egbert
@Egbert According to learn.microsoft.com/en-gb/dotnet/api/… 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.Timberlake
A
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!

Assess answered 19/6, 2015 at 13:30 Comment(2)
Opening a folder. Getting error on line process.EnableRaisingEvents = true;, because process is null.Gorey
@Gorey Make the Process object first, set its EnableRaisingEvents property, and only then Start it?Egbert
F
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.

Flatulent answered 17/11, 2009 at 1:48 Comment(7)
yes, i was getting errors chasing that, didn't know about <strike>strike</strike> btw coolGlenine
which, unfortunately, only works in answers / questions, but not in comments ;-). I just updated.Flatulent
+1 I'll use this code to launch other apps, but the Process.Start() was exactly what I needed.Glenine
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).Flatulent
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 stringGanglion
due to the use of unmanaged Resources should I wrap this use inside another Class implementing IDisposable?Enchant
@LuckyLikey, yes, that's usually a good idea and a good design practice.Flatulent
G
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");
Gladi answered 17/12, 2021 at 19:1 Comment(1)
windows cannot find path, please check the name again.Leeland

© 2022 - 2025 — McMap. All rights reserved.