Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open folder issue

Tags:

c#

directory

I want to open the folder where a file had been just saved and select the file, for that I use this little code:

 var psi = new ProcessStartInfo("Explorer.exe", "/select," + dlg.FileName);
                Process.Start(psi);

It works perfectly.

I need to put this code in several places so I decided to create a method, there is also a condition in this method:

 private static void OpenFolderAndSelectMyFile(string fileName)
 {
     if (MySettings.Default.openFolder == true)
     {
         var psi = new ProcessStartInfo("Explorer.exe", "/select," + fileName);
         psi.WindowStyle = ProcessWindowStyle.Maximized;
         Process.Start(psi);
     }    
 }

This doesn't work as expected: this opens the parent folder (of the folder containing my file). It also selects the folder.

Why this difference in behavior and how to solve it?

like image 783
Sturm Avatar asked Oct 21 '22 03:10

Sturm


1 Answers

The only way for this to occur, is fileName isn't what it was in your original code. The code is exactly the same, and would work as expected, if fileName was in fact what it was in the original code location.

It's likely that fileName is now actually the full path to the folder rather than the full path to the file including the file name.

like image 137
Mike Perrenoud Avatar answered Oct 23 '22 19:10

Mike Perrenoud