Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Process.Start in Core 3.0 does not open a folder just by its name

I have just migrated a desktop app from Framework to Core 3.0.

Process.Start(path_to_folder); workes fine in Framework but throws Win32Exception: Access denied in the Core. Process.Start("explorer.exe", path_to_folder); works fine on both.

Is this a bug or limitation in the Core?

like image 701
yaugenka Avatar asked Nov 13 '19 22:11

yaugenka


People also ask

How do I open a directory in C#?

To open a folder, you just specify folder name without /select, part. Something like explorer c:\folder_name . /select option requires an existing file or folder and open its parent and select the item. Thus both options are available.

How does process start work?

Start(String, String) Starts a process resource by specifying the name of an application and a set of command-line arguments, and associates the resource with a new Process component.

How do I start a process in VB net?

Start another application using your . NET code As a . NET method, Start has a series of overloads, which are different sets of parameters that determine exactly what the method does. The overloads let you specify just about any set of parameters that you might want to pass to another process when it starts.

What is process in C#?

C# Process class provides Start method for launching an exe from code. The Process class is in the System. Diagnostics namespace that has methods to run a .exe file to see any document or a webpage. The Process class provides Start methods for launching another application in the C# Programming.


1 Answers

I suspect the ProcessStartInfo.UseShellExecute property is what would allow this to work on .NET Framework. From the documentation...

The default is true on .NET Framework apps and false on .NET Core apps.

...and the Process.Start() overloads that just take string parameters will likely just leave that property at the default. To work around this, create your own ProcessStartInfo with the UseShellExecute property set to true and pass that to an overload of Process.Start() instead...

ProcessStartInfo startInfo = new ProcessStartInfo(path_to_folder) {
    UseShellExecute = true
};

Process.Start(startInfo);

For completeness, when I try running this...

Process.Start(Environment.SystemDirectory);

...in .NET Core 3.0 I get this exception...

Unhandled exception. System.ComponentModel.Win32Exception (5): Access is denied.

  • at System.Diagnostics.Process.StartWithCreateProcess(ProcessStartInfo startInfo)
  • at System.Diagnostics.Process.Start()
  • at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
  • at System.Diagnostics.Process.Start(String fileName)

The missing link between Process.Start() and Process.StartWithCreateProcess(ProcessStartInfo startInfo) is Process.StartCore(ProcessStartInfo startInfo), which branches based on the value of UseShellExecute and I imagine gets inlined. The exception appears to be thrown after a call to CreateProcess(), presumably because a directory path is specified as the file to executable.

Note that if you pass a path to a non-executable file to that same Process.Start(String fileName) overload the exception message becomes "The specified executable is not a valid application for this OS platform."

The reason why calling Process.Start(String fileName, String arguments) works despite following largely the same code path is because the ProcessStartInfo instance it creates under the covers has a FileName property that does refer to a file (explorer.exe) that can be directly executed even if UseShellExecute is false.

like image 85
Lance U. Matthews Avatar answered Jan 21 '23 19:01

Lance U. Matthews