0

I have searched a lot on this topic on internet but can find a decent solution to it.

I want my application to get and set Environment Variable values as soon as it is started but it seems that i need administrator privileges for it. Now the problem is that i cannot set the EXE to "Run As Administrator" because on starting the application, it always prompts for admin permission.

Is there any way to avoid the admin privileges prompt programatically?

Here are the following articles i tried which didn't work :-

  1. How do I force my .NET application to run as administrator?
  2. Disabling UAC programmatically
  3. How can I change never notify in 'user account control settings'
Community
  • 1
  • 1
Agent_Spock
  • 1,107
  • 2
  • 16
  • 44
  • 4
    UAC would be pretty pointless if applications could just override it... – Equalsk Jun 28 '16 at 09:20
  • You can change the manifest of your software to ask for Admin rights when running by default. See the following answer how to achieve it: http://stackoverflow.com/a/2818776/1600654 And no, if UAC is enabled, you cannot do it silently. – Alex Jun 28 '16 at 09:22
  • Hiding UAC programmatically is impossible. But you could hide it if your application is trusted by 'TrustedInstaller'. –  Jun 28 '16 at 09:45
  • @Alex i have already seen it but the problem is it for admin rights every time i use the software – Agent_Spock Jun 28 '16 at 10:25
  • @Equalsk i just need it for my application but i cant find an app that provides the same functionality with a specific app. – Agent_Spock Jun 28 '16 at 10:28
  • @shad0wk can you tell me how can i get my application trusted from "TrustedInstaller"? – Agent_Spock Jun 28 '16 at 10:33
  • 3
    You can't find one because it's not possible. If an application could just always silently run as admin or click "Yes" to the prompt then UAC would be totally pointless. Your application will just have to prompt each time it runs, just like everyone else. – Equalsk Jun 28 '16 at 10:36
  • Umm, I don't really know but I'm sure it's when an application is being installed. If you open File Explorer `explorer.exe` properties and go the security tab you can see the name "TrustedInstaller". That's why File Explorer can change so many things. –  Jun 28 '16 at 10:37
  • @Equalsk the problem with UAC prompt is that my client uses 'NComputing' and the admin password is only known to their IT sector. Now to run the software if they have to contact their IT sector each time, i don't think this is very feasible. – Agent_Spock Jun 28 '16 at 10:46
  • @shad0wk i have seen applications that are published by Microsoft and are 'TrustedInstaller' but they still require the admin rights to run. – Agent_Spock Jun 28 '16 at 10:48
  • 2
    With respect having to contact their IT department each time is their problem and not yours. This is the way the framework is designed for security, you can't change that. Set the clients expectations or find a way to live without this value. – Equalsk Jun 28 '16 at 11:40
  • @Equalsk the main problem is that their IT department is not with them and if they contact their IT department they send a person 1 day later because they are situated far away from the office and just to run the software you have to wait 1 day. That is not possible. – Agent_Spock Jun 29 '16 at 04:36
  • 1
    It's also not possible to override UAC prompting. Either store the value for use next time, find a way to live without it or accept that their IT is slow. – Equalsk Jun 29 '16 at 09:05
  • @Equalsk can you suggest me some places where i can store some values which is essential for software (not the database itself) like registry, app data? – Agent_Spock Jun 29 '16 at 11:31
  • Read about using Settings, there are literally hundreds of guides on it. – Equalsk Jun 29 '16 at 11:34

1 Answers1

0

Is there any way to avoid the admin privileges prompt programatically?

Yes, but you should append keys to registry as shown in second link article. And it's not a promt disable, it's UAC disable. If UAC is enabled, windows will always ask you for admin rights, even you try to get them programmatically.

And what about Env Vars, they only divided on groups for your user and for all users, not admin. So you must be able to access all of shared env vars

Daniil Vlasenko
  • 457
  • 3
  • 11
  • The error i am receiving is "Requested registry access is not allowed." and here is my code for it "Environment.SetEnvironmentVariable("MyAppName", MyValue, EnvironmentVariableTarget.Machine);" – Agent_Spock Jun 28 '16 at 10:32
  • Sorry, didn't see that you want set var too. Yep, to set machine env var, you need admin rights. You can run something with admin rights and without prompt ony if you'll turn off UAC. It seems there is no another way – Daniil Vlasenko Jun 28 '16 at 11:11