1

I would like to open a folder using .NET and it seems like as if Process.Start is the way to go: Open a folder using Process.Start.

I already read How to securely use Process.Start? but it deals with user input and was answered almost six years ago, it might be outdated.

I have a certain path like the following which is not based on any user input:

  • C:\Users\cow
  • C:\Users\cow\

What I already learned is to check whether the path is acually a folder path i.e. ends with a backslash. Another workaorund would be to specify "explorer.exe" directly. I decided to do both and came up with the following:

var path = @"C:\Users\cow";
if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
{ //To avoid things like C:\Users\cow.exe
    path += Path.DirectorySeparatorChar;
}
Process.Start("explorer.exe", path);

What else should I take in consideration?

0lli.rocks
  • 143
  • 7
  • It depends on how/why you want to open the folder. There are several file dialogue components in .Netyou can use. You can also use URI handlers for this. I think this is a XY problem and you're asking the wrong question, you would be better off asking "How do I achieve $context specific file/folder operation on stack overflow". – wireghoul Feb 06 '20 at 11:29
  • @wireghoul I want to open the folder so that the user does not have to navigate to this folder. I don't want to make any further actions after that, it's up to the user. We also show the path to the user, so he/she could open the path on his/her own, but our users should focus on our application rather than opening paths. – 0lli.rocks Feb 06 '20 at 11:38

0 Answers0