How can I open a folder in Windows Explorer?

Sam Beach picture Sam Beach · Sep 4, 2015 · Viewed 16.3k times · Source

I don't need any kind of interface. I just need the program to be an .exe file that opens a directory (eg. F:).

What kind of template would I use in C#? Would something other than Visual Studio work better? Would a different process entirely work better?

Answer

voytek picture voytek · Sep 4, 2015

In C# you can do just that:

Process.Start(@"c:\users\");

This line will throw Win32Exception when folder doesn't exists. If you'll use Process.Start("explorer.exe", @"C:\folder\"); it will just opened another folder (if the one you specified doesn't exists).

So if you want to open the folder ONLY when it exists, you should do:

try
{
    Process.Start(@"c:\users22222\");
}
catch (Win32Exception win32Exception)
{
    //The system cannot find the file specified...
    Console.WriteLine(win32Exception.Message);
}