Search code examples
c#selectwindows-explorer

C# - problems opening explorer with audio file selected using "/select, <file>"


I'm working on fixing a feature in a c#.net app where the user can right click on a file name and they'll have an "Open in Explorer" option. When they click it a new explorer window opens up with that file selected.

The problem is that if the feature is used to open an audio file, then it seems to infrequently work. The first few attempts will appear selected in an explorer window with no issue.

However, every once in a while the explorer window will hang for half a second or so as it opens, and no file will be selected when it resumes. If you then go back to the app and try it again on that same file, then explorer will open with the file selected; no issue.

So far, it appears to work without fail for text based files and directories. Only audio files have this issue.

I've tried most of the stuff already on stackoverflow concerning the "/select" CL arg, but none of it has solved the issue. Any ideas?

The current code attempt:

void MyFunc(object sender, EventArgs e)
{
    string filePath= "";

    // Code that gets full filepath is here - it has so far always returned a correct path

    if (!string.IsNullOrEmpty(filePath))
    {
        filePath = filePath.Replace(@"/", @"\");

        if (File.Exists(filePath))
        {
            string args = string.Format("/select,\"{0}\"", filePath);

            Process.Start("explorer.exe", args);
        }
    }
}

Extra information:

  • using C#.net 5.0
  • visual studio 2015
  • Windows 10
  • the program only currently recognizes .wav files and a custom text based file type.

thanks


Solution

  • After talking with more people, we found out it was a quirk with viewing music files in Explorer.exe. I ended up using this as a mitigation, emailing the couple of users, and including instructions on the help doc.

    • Opened explorer and went to the base directory that my application searched for files.
    • Right clicked on the file pane and select View->Details.
    • Selected "Customize This Folder" and go to the "Customize" tab. Set the "Optimize this folder for:" dropdown selection to "General Items".
    • Checked "Also apply this template to all subfolders".
    • Clicked "OK". Finished.