none
Opening folder in explorer by C# code

    Question

  • When I use the following code, a new instance of "explorer.exe" starts and it takes time and extra memory to start. But when I use command line in "cmd" and write "explorer" no new process starts. So it does not take time. How can I open any folder in explorer without starting a new instance process of explorer by C# code?

     

    System.Diagnostics.Process process = new System.Diagnostics.Process();

    process.StartInfo.UseShellExecute = true;

    process.StartInfo.FileName = @"explorer";

    process.StartInfo.Arguments = @" ";

    process.Start();

     

    Friday, April 11, 2008 5:50 AM

Answers

All replies

  • You can get the already running explorer process instead of starting a new one.

    http://msdn2.microsoft.com/en-us/library/system.diagnostics.process.getprocessesbyname(VS.71).aspx
    Friday, April 11, 2008 7:01 AM
  •  

    Have you tried simply:

     

    Process.Start(@"c:\windows\");

     

    etc?

    Friday, April 11, 2008 7:03 AM
  • Marc is right, simply putting

    Code Snippet

    Process.Start(@"c:\"); //obviously replace with your own path :)


    Will do it
    Friday, April 11, 2008 7:24 AM
  • In your solution I cannot send arguments to explorer like this:

     

    Process.Start(@"c:\windows", @"/e,/select,c:\windows\Config");

    Saturday, April 12, 2008 6:47 AM
  • As far as I know, and TBH I very well may be wrong, but the only way to select a file like that is to open a new explorer process..

    Saturday, April 12, 2008 8:40 AM
  • Sorry to be resurrecting this old post, however, on windows 7 it appears that article KB152457 http://support.microsoft.com/kb/152457 has some bearing on this question, with regard passing parameters.

    -------

    If you were to utilise the command (for instance from task manager):

     

     

    explorer /root,c:\Artk /select,c:\ArtWork\Logo.bmp

     

    Instead of a new explorer window opening and the bitmap being highlighted in the list of files present within the folder, the image is actually opened with the picture viewer tool.  If I do this with a PDF then the default action for a PDF is carried out, the same with TXT and DOC etc etc.

     

    Instead of just highlighting the item on screen the item's default associated action is carried out.

    -------

    Now, as for sending this commands as parameters to explorer.exe in C# code, we can not simply use "Process.Start" as you have discovered, instead we need to use a "ProcessStartInfo" in conjunction with a new "Process" thus:

     

    /// <summary>
    /// Launch the folder view
    /// </summary>
    /// <param name="p_Filename">The fully qualified path and filename of the target to open as highlighted by explorer</param>
    private bool LaunchFolderView(string p_Filename)
    {
     bool l_result = false;
    
     // Check the file exists
     if ( File.Exists(p_Filename) )
     {    
      // Check the folder we get from the file exists
      // this function would just get "C:\Hello" from
      // an input of "C:\Hello\World.txt"
      string l_folder = FileSystemHelpers.GetPathFromQualifiedPath(p_Filename);
    
      // Check the folder exists
      if ( Directory.Exists(l_folder) )
      {
       try 
       {
        // Start a new process for explorer
        // in this location     
        ProcessStartInfo l_psi = new ProcessStartInfo();
        l_psi.FileName = "explorer";
        l_psi.Arguments = string.Format("/root,{0} /select,{1}", l_folder, p_Filename);
        l_psi.UseShellExecute = true;
    
        Process l_newProcess = new Process();
        l_newProcess.StartInfo = l_psi;
        l_newProcess.Start();
    
        // No error
        l_result = true;
       }
       catch (Exception exception)
       {
        throw new Exception("Error in 'LaunchFolderView'.", exception);
       }
      }
     }
    
     return l_result;
    }
    

    This works, however, it still falls foul of automatically starting the default action for the selected object.
    • Edited by Xelous Wednesday, August 10, 2011 2:35 PM Missed last line from explanation text post code.
    • Proposed as answer by Hans Hagenes Thursday, August 16, 2012 2:39 PM
    Wednesday, August 10, 2011 2:32 PM
  • This works:

    ...

    l_psi.Arguments = string.Format("{0},/select", p_Filename),

    ...

    Thursday, August 16, 2012 2:45 PM