Open a folder using Process.Start
Have you made sure that the folder "c:\teste
" exists? If it doesn't, explorer will open showing some default folder (in my case "C:\Users\[user name]\Documents
").
Update
I have tried the following variations:
// opens the folder in explorer
Process.Start(@"c:\temp");
// opens the folder in explorer
Process.Start("explorer.exe", @"c:\temp");
// throws exception
Process.Start(@"c:\does_not_exist");
// opens explorer, showing some other folder)
Process.Start("explorer.exe", @"c:\does_not_exist");
If none of these (well, except the one that throws an exception) work on your computer, I don't think that the problem lies in the code, but in the environment. If that is the case, I would try one (or both) of the following:
- Open the Run dialog, enter "explorer.exe" and hit enter
- Open a command prompt, type "explorer.exe" and hit enter
Process.Start(explorer.exe, Path) can't open the right folder
Windows paths use \
not /
. They are often interchangable, but not always.
This
C:\>explorer.exe "C:\Users\David\Documents\My Games\FarmingSimulator2022\mods"
works. And so this does too:
System.Diagnostics.Process.Start("explorer.exe", @"""C:\Users\David\Documents\My Games\FarmingSimulator2022\mods""");
Process.Start in Core 3.0 does not open a folder just by its name
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 andfalse
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
.
Process.Start opening explorer
I restarted my computer and it seems to be working now. Thanks for the help anyway
Opening a folder in explorer and selecting a file
Use this method:
Process.Start(String, String)
First argument is an application (explorer.exe), second method argument are arguments of the application you run.
For example:
in CMD:
explorer.exe -p
in C#:
Process.Start("explorer.exe", "-p")
Access is denied exception when using Process.Start() to open folder
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.
Related Topics
Execute Multiple Queries in Single Oracle Command in C#
Do Using Statements and Await Keywords Play Nicely in C#
How to Play a Sound in an ASP.NET Web Page
Fastest Way to Remove Duplicate Value from a List<> by Lambda
How to to Return an Image with Web API Get Method
How to Convert a System.Io.Stream to a Windows.Storage.Streams.Irandomaccessstream
Center Messagebox in Parent Form
Getting Correct Image Rotation
How to Make Form1 Label.Text Change When Checkbox on Form2 Is Checked
In-Memory Database Doesn't Save Data
Convert Datatable to Ienumerable<T>
How to Loop Over the Properties of a Class
Checking If an Object Is a Number in C#
How to Dynamically Change Auto Complete Entries in a C# Combobox or Textbox