Jul 27, 2012

How to start a Process in administrator mode - C#

3 comments
Previosuly I wrote an article on how to create UAC Compatible applications in .NET. In that UAC will be applicable for the whole application. Today I got a scenario like to install a Windows service from my application, at the same time I don’t want to enable UAC for the whole application.
Here, I am using the Process.Start() method for invoking the TestService.exe. As I am not running this application as Administrator, getting an access denied message. Then I figured out the solution with Verb property of ProcessStartInfo. Simply set the Verb property of ProcessStartInfo as “runas”, this will popup the UAC dialog, before starting the application. If the user press Yes, it will launch the application otherwise it will throw a WIN32Exception.


Example: In this sample i have taken application as notepad.exe 

var processStartInfo = new ProcessStartInfo(@"C:\Windows\Notepad.exe");
processStartInfo.Verb = "runas";
try
{
    Process.Start(processStartInfo);
}
catch (Win32Exception ex)
{
    MessageBox.Show(ex.ToString(), "Run As",
        MessageBoxButtons.OK, MessageBoxIcon.Error);
}

You can also check the OS platform also, this is required only, if you are using Vista or above. Check this using Environment.OSVersion.Version.Major, if it is more than 6, it is Vista or above.

if (Environment.OSVersion.Version.Major >= 6)
{
    processStartInfo.Verb = "runas";
}

We are done now.

If you feel this is helpful or you like it, Please share this using share buttons available on page.

3 comments :

  1. Thanks for your great information, the contents are quiet interesting.
    access Mp3lemon in UK

    ReplyDelete
  2. We are using wkhtmltopdf.exe (up to 10 months it had been worked with no issues) but from few days we are facing issue with wkhtmltopdf.exe. For now we are getting error from wkhtmltopdf.exe and PDF is not generating for us, we use JavaScripts. When I click page is going to our written error page. Our problem in PDF is not coming with any versions from wkhtmltopdf.exe. Our code and applications are very clear .wkhtmltopdf.exe is working in local why not in live server(Windows 2008 R2 enterprise)? We had tried all the versions from (wkhtmltopdf site)[http://wkhtmltopdf.org/downloads.html] all were worked in localhost but not in live server.our local server iis version is (7.5) is same as in live server is about iis version(7.5) what to do next ?? in localhost pdf is generating with iis version is (7.5) having same version in Windows server 2008 R2 enterprise but PDF is not generating.

    protected void Export(object sender, EventArgs e)
    {
    String GlobalDomainName = System.Configuration.ConfigurationManager.AppSettings["live_domainName"];

    string url = "PropertyEWC.aspx?id=" + PropertyId + "&bid=" + UserData.BranchId + "&aid=" + UserData.CompanyId;


    string args = string.Format("\"{0}\" - ", GlobalDomainName + url);
    var startInfo = new ProcessStartInfo("C:\\Program Files\\wkhtmltopdf1\\bin\\wkhtmltopdf.exe", args)
    {

    UseShellExecute = false,
    CreateNoWindow = true,
    RedirectStandardOutput = true

    };
    var proc = new Process { StartInfo = startInfo };
    proc.Start();
    string output = proc.StandardOutput.ReadToEnd();
    byte[] buffer = proc.StandardOutput.CurrentEncoding.GetBytes(output);
    proc.WaitForExit();
    proc.Close();
    Response.ContentType = "application/pdf";
    Response.AddHeader("Content-Disposition", "attachment;filename=ElectronicWindow.pdf");
    Response.BinaryWrite(buffer);
    Response.End();
    }

    In above code we are getting error in proc.start() what to do for solving the error is their any alternative method for proc.start.Both local and Live server IIS Version is about (7.5),proc.start() runs in localhost but not in windows server 2008 R2.

    ReplyDelete