Search code examples
c#excelexcel-dna

Is it possible to open Windows Explorer from a linklabel


Good Morning All,

I have a project which will be distributed as an Excel add-in, it is written in C# and uses ExcelDNA.

What I would like to do is add a linklabel to my add-in main form, when clicked, I would like this to open windows explorer at a specific index, e.g. the G: drive.

Is this possible? I've seen some examples but haven't been able to wrangle them,

Thanks in advance!


Solution

  • Absolutely:

    From the Microsoft KB:

       Option            Function
       ----------------------------------------------------------------------   
       /n                Opens a new single-pane window for the default
                         selection. This is usually the root of the drive that
                         Windows is installed on. If the window is already
                         open, a duplicate opens.
    
       /e                Opens Windows Explorer in its default view.
    
       /root,<object>    Opens a window view of the specified object.
    
       /select,<object>  Opens a window view with the specified folder, file,
                         or program selected.
    
       Examples
       -----------------------------------------------------------------------
       Example 1: Explorer /select,C:\TestDir\TestProg.exe
                  Opens a window view with TestProg selected.
    
       Example 2: Explorer /e,/root,C:\TestDir\TestProg.exe
                  Opens Explorer with drive C expanded and TestProg selected.
    
       Example 3: Explorer /root,\\TestSvr\TestShare
                  Opens a window view of the specified share.
    
       Example 4: Explorer /root,\\TestSvr\TestShare,select,TestProg.exe
                  Opens a window view of the specified share with TestProg selected.
    

    So you can call Explorer /select,G:\yourfile to open the explorer with that file/directory selected.

    Put that call in Process.Start() et voila :)