Opening a folder in explorer programmically

When opening a folder in explorer by using Process.Start() method a new process of windows explorer will be created. So in task manager we will see more than one ‘explorer.exe’. Inorder to avoid this, use
ShellExecute() instead.
The actual definition of ShellExecute is like below

HINSTANCE ShellExecute(
    HWND  hwnd,       // handle to parent window
    LPCTSTR  lpOperation,     // pointer to string that specifies operation to perform
    LPCTSTR  lpFile,   // pointer to filename string
    LPTSTR  lpParameters,      // pointer to string that specifies executable-file parameters
    LPCTSTR  lpDirectory,      // pointer to string that specifies default directory
    INT  nShowCmd    // whether file is shown when opened
   );

In C# .NET we can use the above like

IntPtr ShellExecute(
    IntPtr hwnd,           // handle to parent window
    string  lpOperation, // string that specifies operation to perform
    string  lpFile,           // filename
    string  lpParameters,            // string that specifies executable-file parameters
    string  lpDirectory,  // string that specifies default directory
    int  nShowCmd       // whether file is shown when opened
   );

Let's start now

First Add this reference to your code


Using System.Runtime.InteropServices;


Inside the class declare the constants


private const int SW_NORMAL = 1;  // Window style - normal
private const int SW_MAXIMIZE = 3;  // Window style - maximized
private const int SW_MINIMIZE = 6;   // Window style - minimized



The above external function can be declared in our class as below

[DllImport("shell32.dll")]
public static extern IntPtr ShellExecute(IntPtr hwnd,string lpOperation,string
lpFile,string lpParameters,string lpDirectory,int nShowCmd);


here lpOperation should be either “open” or “explore” if the lpFile is a folder.
If lpFile is a file then use only “open”.


That’s it!

Now we can use this function in our project

Examples are given below.
 

//Open “windows” folder in explorer
private void button1_Click(object sender, EventArgs e)
{
     ShellExecute(this.Handle, "open", @"c:\windows", null, null, SW_MORMAL);
}


//explore “windows” folder in explorer

private void button1_Click(object sender, EventArgs e)
{
     ShellExecute(this.Handle, "explore", @"c:\windows", null, null, SW_MORMAL);
}


//Open “windows” folder in explorer (Maximized window)
private void button1_Click(object sender, EventArgs e)
{
     ShellExecute(this.Handle, "open", @"c:\windows", null, null, SW_MAXIMIZE);
}


//Open “windows” folder in explorer (Minimized window)
private void button1_Click(object sender, EventArgs e)
{
     ShellExecute(this.Handle, "open", @"c:\windows", null, null, SW_MINIMIZE);
}


//Open “notepad” in normal window
private void button1_Click(object sender, EventArgs e)
{
     ShellExecute(this.Handle, "open", @"c:\windows\notepad.exe", null, null, SW_NORMAL);
}


//Open “notepad” in maximized window
private void button1_Click(object sender, EventArgs e)
{
     ShellExecute(this.Handle, "open", @"c:\windows\notepad.exe", null, null, SW_MAXIMIZE);
}



Comments