Search code examples
c#winformsprocessactive-directory

How can you launch dsa.msc programmatically through a button click event in C#?


I am trying to open Active Directory using a button on my application. I don't want to get or set information from or in AD, just open it. The code below gives the error in System.dll, which isn't very helpful:

Exception thrown: 'System.ComponentModel.Win32Exception'

Any ideas?

private void btnAD_Click(object sender, EventArgs e)
{
    try
    {
        Process procAD = new Process();
        procAD.StartInfo.FileName = "C:\\Windows\\System32\\dsa.msc";
        procAD.Start();
    }
    catch
    {
        Console.WriteLine("Didn't open...");
    }
}

Solution

  • There's a few ways. I would suggest that you actually start the mmc.exe executable and give it the msc file as an argument. For example:

    Process procAD = new Process();
    procAD.StartInfo.FileName = "C:\\Windows\\System32\\mmc.exe";
    procAD.StartInfo.Arguments = "C:\\Windows\\System32\\dsa.msc";
    procAD.Start();
    

    You should also be able to do this, which is the same a double-clicking the file:

    System.Diagnostics.Process.Start("C:\\Windows\\System32\\dsa.msc");