Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access is denied exception when using Process.Start() to open folder

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

like image 708
IRAndreas Avatar asked Jan 27 '16 08:01

IRAndreas


3 Answers

According to MSDN (https://msdn.microsoft.com/en-us/library/53ezey2s(v=vs.110).aspx) the System.Diagnostics.Process.Start(string) runs the file or process (and therefore does not open the folder). For opening a folder, the https://msdn.microsoft.com/en-us/library/h6ak8zt5(v=vs.110).aspx 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.

like image 152
Rao Avatar answered Nov 20 '22 02:11

Rao


Try this:

var psi = new System.Diagnostics.ProcessStartInfo() { FileName = pathToFolder, UseShellExecute = true };
System.Diagnostics.Process.Start(psi);
like image 28
arkhivania Avatar answered Nov 20 '22 02:11

arkhivania


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.

like image 25
Arie Avatar answered Nov 20 '22 01:11

Arie