标签云

微信群

扫码加入我们

WeChat QR Code

Once my program is installed on a client machine, how do I force my program to run as an administrator on Windows 7?


Besides what Binary Worrier wrote, you might want to write some code to test if you have admin privileges .. (is that what you are asking for?)

2019年10月20日30分37秒

I would not take this task lightly though, you should verfiy what it actually needs admin for and see if you can work around it. No customer is going to be happy running an app in admin mode all the time. Alot of bigger customers won't even consider an app like that, and if logo testing matters to you it will not pass like that.

2019年10月20日30分37秒

Alex is very much on point. If possible, only elevate when necessary, otherwise, Group Policy, UAC and a number of other variables come into play. At the very least, with UAC, the user would have to authorize on every run as opposed to only when a specific user action is performed.

2019年10月21日30分37秒

The correct way is to embedd a manifest file into your application.

2019年10月20日30分37秒

If you get a ClickOnce error when trying to compile, see this answer: stackoverflow.com/questions/11023998/…

2019年10月21日30分37秒

Your project has to be set up to use the app manifest too - in Project Properties, check the "Application" tab and make sure the "Manifest:" under 'Resources" is set to your app.manifest (or whatever you named the .manifest file).

2019年10月20日30分37秒

I had to reload the project before VS would prompt me to restart in admin mode.

2019年10月20日30分37秒

Alejandro - Yes, UAC can be disabled, but when that is, the app will automatically run as administrator (assuming your user has administrator privileges), because disabling UAC means everything runs at the highest privilege the user is allowed.It's kind of like complaining that if you install a fancy lock on the door, it won't work if the door is removed.

2019年10月20日30分37秒

ErikFunkenbusch It won't "automatically run as administrator", it'll run under the normal permissions of the user (admin if the user is admin, or standard if the user is standard). Relying on that particular case, even if it's the default, is what good programs will avoid like the plague. Following your analogy, the fancy lock is nice and all, but properly designed software must anticipate the case that the whole door is removed, even if it's a rare occurrence.

2019年10月20日30分37秒

You could also use <requestedExecutionLevellevel="highestAvailable" uiAccess="false" /> as well

2019年10月20日30分37秒

MarkKram: What does highestAvailable have to do with this? The question is about forcing admin, highestAvailable is less restrictive than requireAdministrator and will let a non-admin user start the app un-elevated with no UAC prompt, only admins will get prompted...

2019年10月20日30分37秒

Here is an MSDN example of the IsInRole, Anders talks about.

2019年10月20日30分37秒

The manifest entry is ignored if UAC is disabled.

2019年10月20日30分37秒

I have set EnableLUA to 0 on Server 2008 R2 and removed myself from the Administrators group, rebooted, and now an exe that specifies level="requireAdministrator" runs without any prompt

2019年10月20日30分37秒

This only detects if the context is running as Admin it does not forec the application to run as Admin as requested by the OP

2019年10月20日30分37秒

I don't think there is any programmatic way to force an application to elevate its own perms. If there were, that would be quite the security risk, no?

2019年10月20日30分37秒

Although your solution is good but question was different. ;)

2019年10月20日30分37秒

see a refactored version of this method here stackoverflow.com/a/50186997 (subjective)

2019年10月20日30分37秒

This does not answer the question!

2019年10月20日30分37秒

This works. However, it made a blank cmd window appear when running the CMD application (using c# cmd app to run some exe in the background).

2019年10月20日30分37秒

This answer is about VB.NET :-) , not VS 2010 in general. The "Add New Item" answers are about C#. In C++ you can do it in project settings.

2019年10月20日30分37秒

+1 for code-only approach. Note that you need UAC enabled for having a chance to launch anything with runas as administrator from a non-admin user, otherwise it will open silently with current user permissions (checked on windows 7 64 bit). As far as I can tell the only thing you can do with UAC disabled and the admin right is missing is to stop execution in a proper moment.

2019年10月20日30分37秒

This does not answer the question!

2019年10月20日30分37秒

Elmue it is more logical to add your comment to the original answer that I just refactored, you can find a link to that answer in mine.

2019年10月21日30分37秒

Great yochai Timmerthanks.

2019年10月20日30分37秒

This doesn't answer the question. "Once my program is installed on a client machine", not "how do I".

2019年10月20日30分37秒

Sorry for being unclear. Once your program is installed, change this setting in your executable's properties (your main program, not the installer).He does want to force his program to run as admin.

2019年10月20日30分37秒

Its better to set the admin requirement in the manifest. I'd argue it answers the question, but just barely.

2019年10月20日30分37秒

Joe To be fair, the accepted answer doesn't answer the OPs question as requires you to reinstall the application exe. That's hardly a solution for "Once my program is installed". If anything this downvoted answer is more correct than the accepted answer, so I don't understand why that answer has 400+ votes.

2019年10月20日30分37秒

but do you see the tags ? Your post is not the solution, even the answer...

2019年10月20日30分37秒