26

I have a winforms application in C# where I have to open a certain Folder. I use

System.Diagnostics.Process.Start(pathToFolder);

This results in the following exception:

System.ComponentModel.Win32Exception (0x80004005): Access is denied

at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo startInfo)

at System.Diagnostics.Process.Start()

at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)

at MyApp.openLogFolderToolStripMenuItem_Click(Object sender, EventArgs e)

I have already checked the following things:

  • The folder exists
  • The user has rights to the folder (can open it in Explorer)

Another thing is that if I use Process.Start() to open a file inside this folder, it works.

Can anyone give me a hint?
Cheers

Edit My goal is to open the folder in Explorer. The pathToFolder is something like H:\Something\App.Name\Log

3
  • 5
    Not sure what most folks commenting & answering are talking about when they reiterate that Process.Start won't open a folder path. It does. I work on a .NET Framework app that has used this feature for years. I'm here because I just migrated the app to .NET Core, and now I'm getting the "access is denied" error.
    – William
    Commented Jun 12, 2020 at 21:15
  • @William Confirming. The problem is not present in .NET Framework where Process.Start(directory) is sufficient. But it yields the error in .NET Core. Commented Oct 29, 2023 at 16:58
  • Maybe a permission issue, have you tried to run VS as admin? Commented Jun 18 at 7:39

7 Answers 7

27

According to Microsoft Doc's the System.Diagnostics.Process.Start(string) runs the file or process (and therefore does not open the folder). For opening a folder, this doc page sugests that you might do this with System.Diagnostics.Process.Start(string, string) where first should be a way to explorer, Total commander or something similar, and second should be a argument telling the used explorer what to do (open the folder pathToFolder).

I suppose that some system variable stores the value for "default folder viewer" but I do not know where. I will try to go for it and return later with the answer.

Hope that it helps.


EDIT: I did some quick digging around and to open the folder the following should do the trick:

System.Diagnostics.Process.Start(Environment.GetEnvironmentVariable("WINDIR") +
    @"\explorer.exe", pathToFolder);

Where first argument is a path to classical windows explorer and second is the actual path to the folder itself. It seem that widows does not by itself hold path to other "folder viewer" (such as Total Commander etc.), so this way is probably off the table.

2
  • 9
    Could solve the problem by simply calling Process.Start("explorer.exe", pathToFolder); instead of Process.Start(pathToFolder);
    – IRAndreas
    Commented Jan 27, 2016 at 10:20
  • Works but a minor change I would make is to use Environment.GetFolderPath(Environment.SpecialFolder.Windows) instead of `Environment.GetEnvironmentVariouable("WINDIR") to avoid the magic string, but that's just me.
    – BryanJ
    Commented May 24 at 16:24
26

Try this:

var psi = new System.Diagnostics.ProcessStartInfo()
{
    FileName = pathToFolder,
    UseShellExecute = true
};
System.Diagnostics.Process.Start(psi);
2
  • 2
    This is the best way! One reason for that is that if you call this multiple times, it wont reopen the folder multiple times and will reuse the original window
    – Raj Rao
    Commented Aug 19, 2020 at 22:14
  • This works well when opening directories that require elevated permissions.
    – Brien King
    Commented Jul 31, 2021 at 20:11
2

I usually use this to open file/directory:

    public static void OpenFile(string path, bool isDirectory = false)
    {
        if (string.IsNullOrEmpty(path)) return;
        if ((isDirectory && Directory.Exists(path)) || (!isDirectory && File.Exists(path)))
        {
            ProcessStartInfo pi = new ProcessStartInfo(path);
            pi.Arguments = Path.GetFileName(path);
            pi.UseShellExecute = true;
            pi.WindowStyle = ProcessWindowStyle.Normal;
            pi.Verb = "OPEN";

            Process proc = new Process();
            proc.StartInfo = pi;

            proc.Start();
        }
    } 

or

Process.Start("explorer.exe",path);

If this doesn't work it may be a permission issue after all.

0

You can set the working directory like this but you can't run the directory itself only files or exe

var startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = //working directory 
Process proc = Process.Start(startInfo);
0

For me it was that I didnt fully Get or Put the full path leading to the .exe For example Path that I get using RegistryKeys:

Process.Start(@"C:\Program Files\WinRAR")   //I get access denied Error

Because It doesnt have at the end the Application name and with or without .exe To get execute the WinRAR app I would need to add again WinRAR.exe at the end of the path or just WinRAR (.exe is optional):

Process.Start(@"C:\Program Files\WinRAR\WinRAR.exe")  //This woks and it opens any program
Process.Start(@"C:\Program Files (x86)\GZ Systems\PureVPN\PureVPN");

You can tweak it as you want I just added the Same name thats at the End of the File path with *backslash *

2
  • The OP wanted to open the folder in the system application, not run the executable inside :) Commented Apr 26 at 10:59
  • @CoderMuffin oh yay I just wanted to make a comment since it was the first question that I pops up when you search it and didnt have relative answers to it that I was searching for so I decided to reply on this one and Maybe it can Help someone that was searching for this but didnt get and exact answer since there isnt even though its not the same as the OPs question. Hope it helps someone Commented Apr 27 at 16:23
0

Very important is to set the working directory before starting the process

Directory.SetCurrentDirectory("BackUp");
Process.Start("BackUp/MyApp.exe");

If you don't do this, the BackUp/MyApp.exe will act as if the current directory is the directory where you're running the Process.Start("BackUp/MyApp.exe") from. And this can cause the above issue, if your code in the process you started is trying to do something in the wrong folder.

-2

This error actually happens when there is a difference between the default behaviour of opening the file and the relative behaviour of opening the file. For example, if you have selected the default application to open .pdf files as Internet Explorer, and you are trying to open the same file using Process.Start() method. You will receive an exception because as per the default operations it should open that file in Internet Explorer and your application is trying to open it using Adobe reader.

To rectify this set the default application for the .pdf file as Adobe Reader and you won't receive this error any more. You can do this by, right-clicking on the file and then select, Default program or App. Further, select the default Program or App from the list of available programs and then select the Always use the selected program/App to open files of this type.

1
  • 1
    That's not true. Nothing keeps you from opening a file with a different program than the one that is chosen as the default under Windows. Imagine what would happen if there were a ".TXT file monopoly" on your PC! Commented Sep 4, 2018 at 6:30

Not the answer you're looking for? Browse other questions tagged or ask your own question.