Leandro Bardelli
Leandro Bardelli

Reputation: 11578

Developing an application that runs commands as administrator

First of all, yes, I was searching about this topic around the internet, but my case is a little more specific, so this is the reason of my question.

My app is an executable winform app developed in Visual Studio .NET 2005 with Framework 2.0, C#

The app needs run some commands on a command line. This command must run as administrator.

The code that actually works is:

        string output = "";
        Process Consola = new Process();
        ProcessStartInfo ConsolaStartInfo = new ProcessStartInfo();
        ConsolaStartInfo.FileName = "cmd.exe";
        ConsolaStartInfo.WorkingDirectory = System.Environment.SystemDirectory;
        ConsolaStartInfo.UseShellExecute = !NeedOutput;
        ConsolaStartInfo.RedirectStandardOutput = NeedOutput;
        Consola.StartInfo = ConsolaStartInfo;
        ConsolaStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
        ConsolaStartInfo.CreateNoWindow = true;
        ConsolaStartInfo.Arguments = "/C " + CommandToPerform;
        ConsolaStartInfo.Verb = "runas";
        Consola.Start();
        Consola.WaitForExit();

where:

    CommandToPerform is a string var with a command
    NeedOutput is true or false depending if i need a return output or not.
   (This works)

The first tip is: this application works perfectly as is expected if I'm running the Visual Studio as administrator on Windows 7.

The second tip is: this application works perfectly as is expected if I'm running the Visual Studio NOT as administrator, but everytime the command line is executed, the process ask me for security validation on the screen (UAC)

Yes, I tried to work with the manifest but I've a lot of problems to compile it. It returns 9009 error sometimes, a lot of kind of errors.

So my questions are:

1) There's possible to do that without ask to the user for permissions? The application must be silent. This is the real question. If so...

2) How can I define the manifest for my application runs this command as administrator? I miss anything about? It's the same sign the application?

3) Can My app runs these commands as administrator without running as administrator? This is the perfect scenario, the users that will use this app can't be / can't will be administrators.

4) This will work on XP? XP ask for UAC? (I can't test it on XP until I develop a minimal requirement version)

Any kind of idea / solution / tip will be appreciated, and if on the final is not a solution, the better workaround will be mark as answer (I've a 100% rate)

Notes:

EDITED:

But when you run the application, it's prompt (with the common UAC prompt) for permissions

Upvotes: 1

Views: 1585

Answers (2)

Leandro Bardelli
Leandro Bardelli

Reputation: 11578

It can be done doing a little service that runs the command, and the service process installer must be with the property Account on: LocalSystem.

So your application doesn't need any requirement or manifest.

It's working on Win7 and on XP

MSDN Creating a Windows Service

And the note:

The LocalSystem account has broad permissions, including the ability to write to the event log. Use this account with caution, because it might increase your risk of attacks from malicious software. For other tasks, consider using the LocalService account, which acts as a non-privileged user on the local computer and presents anonymous credentials to any remote server.

Upvotes: 0

pyrocumulus
pyrocumulus

Reputation: 9290

Well the answers to questions 1) and 3) are simple: this is impossible. Because if it were possible, the whole UAC would have no use at all.

I don't know about XP compatibility (but I do think XP will ignore the manifest) and unfortunately I have no experience with the UAC prompt and VS2005 either. I only did this in VS2008 and above, so I can't help there either. I did find a guide for VS2005, but it's likely you have seen the same one as well:

Enabling your application for uac on vista.

Upvotes: 2

Related Questions