Conchors
Conchors

Reputation: 23

Using Process.Start to open explorer

Edit: solution found, will note under image at the end of the question

After a bunch of research here on SO, I found that the way to open explorer with a selected file was:

Process.Start("explorer.exe", "/select, " + path);

However when I do this with controlled input, Explorer opens just its main window, however when I harcode the function call to the same value that's in the path variable (In my control test its a text file in C:\Temp) it works. So if I do the above when path is "C:\Temp\test.txt" It does't open explorer in the temp folder, however when I do:

Process.Start("explorer.exe", "/select, C:\\Temp\\test.txt");

It works perfectly, opening explorer and highlighting the file. What is happening here? Is there something wrong with the internal formatting on my string variable or something?

(Additionally, I ran into the same issue using the path variable to open a FileInfo. Hardcoded to the same value would work, but using the variable gave me a "the given path's format is not supported" exception")

Image showing that path and the harcoded value are the same: Image showing that path and the harcoded value are the same

The 2 explorer windows (cropped for Security) are the results of the 2 respective calls. The one with the variable shows te basic explorer main page. The one that's hardcoded shows the file selected as expected.

Edit: There was a hidden Left-To-Right Format character hidden in the front of the string.

Upvotes: 0

Views: 930

Answers (1)

Arthur Grigoryan
Arthur Grigoryan

Reputation: 467

    public static class Program
    {
        static void Main()
        {
            Explore("C:\\Users\\art_g\\Desktop\\Sample.txt");
        }
        static void Explore(string path) =>
            Process.Start("explorer.exe", "/select, " + path);
    }

Works like a charm. Check your path string.

Upvotes: 1

Related Questions