none
How to start a Process as administrator mode

    Question

  • Hi all,

    In Vista, I am triying to assign the following file read-write-modify rights.

    "C:\ProgramData\CompanyName\SomeFolder\somefile.dll"

    I can do that manually by starting the command promt as administrator mode then use the following utility.

    takeown /f "C:\ProgramData\CompanyName\SomeFolder\somefile.dll"
    icacls "C:\ProgramData\CompanyName\SomeFolder\somefile.dll" /grant BUILTIN\Users:RWM

    I need to automate this process as part of the installer so I created a C# wrapper to perform then above tasks but it does not work it it complians that I do not have proper rights. I think reason is because the Process I started is not admin. My question is how can I start a process as admin and perform the task. See the code below:

    1process.StartInfo.FileName = @"C:\Windows\System32\takeown.exe" 
    2process.StartInfo.Arguments =@"C:\ProgramData\CompanyName\SomeFolder\somefile.dll /grant BUILTIN\Users:RWM"; 
    3process.Start(); 
    4process.WaitForExit(); 

    Thanks in advance for your suggestions.

    Kind regards,
    MA


    • Edited by MA_NSW Saturday, August 02, 2008 3:44 AM Line break added
    • Changed type nobugzMVP, Moderator Saturday, August 02, 2008 5:33 PM Trying to close thread
    Saturday, August 02, 2008 3:43 AM

Answers

  • Thanks for the suggestions.

    I have also found you can provide the manifest file to elevate the rights. Its working now after adding the manifest file as showed in the video link below:

    http://channel9.msdn.com/posts/DanielMoth/Build-UAC-aware-apps-with-VS2008/

    Regards,
    MA
    Saturday, August 02, 2008 9:00 AM

All replies

  • You can use ProcessStartInfo.UseShellExecute = true and ProcessStartInfo.Verb = "runas".  This is the documented way of invoking a process as an administrator.

    Sasha Goldshtein | http://blogs.microsoft.co.il/blogs/sasha
    Saturday, August 02, 2008 8:08 AM
  • Thanks for the suggestions.

    I have also found you can provide the manifest file to elevate the rights. Its working now after adding the manifest file as showed in the video link below:

    http://channel9.msdn.com/posts/DanielMoth/Build-UAC-aware-apps-with-VS2008/

    Regards,
    MA
    Saturday, August 02, 2008 9:00 AM