Problem
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:
Windows.OpenExplorer("c:\test");
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);
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 came from the website pinvoke.net.
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.
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();
}
I couldn’t get either of them to work for me:
Process.Start(@"c:\test");
Process.Start("explorer.exe" , @"C:\Users");
Answered by Jesus is Lord
Post is based on https://stackoverflow.com/questions/1746079/how-can-i-open-windows-explorer-to-a-certain-directory-from-within-a-wpf-app