• 0

[C#] Requesting UAC elevation is a bad practice, better alternative?


Question

I know that it's technically a bad practice to embed in the manifest file to require administrator rights but I'm not sure what else to do. I know you can make it to where a process within the program requires admin rights but that is not an option since the process would run every 15 min and the user would have to click accept each time (that would drive me nuts). I have to write to a various number of folders that the user has specified in various parts of their computer (program files, my documents, etc). Program files will not let me write to any of the folders without admin rights (which makes sense I guess). So currently I'm embedding in the program the manifest file to require admin rights. Am I stuck with this option? I have a backup program which backs up my client data, I have no way of knowing ahead of time which folders they will write to.

6 answers to this question

Recommended Posts

  • 0

I know that it's technically a bad practice to embed in the manifest file to require administrator rights but I'm not sure what else to do. I know you can make it to where a process within the program requires admin rights but that is not an option since the process would run every 15 min and the user would have to click accept each time (that would drive me nuts). I have to write to a various number of folders that the user has specified in various parts of their computer (program files, my documents, etc). Program files will not let me write to any of the folders without admin rights (which makes sense I guess). So currently I'm embedding in the program the manifest file to require admin rights. Am I stuck with this option? I have a backup program which backs up my client data, I have no way of knowing ahead of time which folders they will write to.

Here is how Julien Manici accomplishes this task in his code for "Windows7LoginBackgroundChanger":


/// <summary>
/// Execute as administrator the cmd file that enable the login screen change if enable == true.
/// Otherwise, execute the cmd file that disable the login screen change.
/// Shows an UAC prompt.
/// </summary>
bool launchCmd(bool enable)
{
string path = System.IO.Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
try
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";
if (enable)
p.StartInfo.Arguments = "/C \"" + path + "\\enable background change\"";
else
p.StartInfo.Arguments = "/C \"" + path + "\\disable background change\"";
p.StartInfo.Verb = "runas"; //shows the uac prompt
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (enable)
{
p.EnableRaisingEvents = true;
p.Exited += new EventHandler(p_Exited);
}
p.Start();
alreadyAskedPermission = true;
return true;
}
catch (Exception)
{
showMessage(Util.getText("error"), Util.getText("operationCanceled"), "");
}
return false;
}
[/CODE]

  • 0

One option would be to implement your application as a service, which I believe once installed would give it admin rights without triggering a UAC prompt.

Almost correct; you'd have to have the service run as a user (can be SYSTEM) which is an administrator but yes, running it as a service would be more than ideal.

  • 0

If your app will run often, do it as a service.

Otherwise, don't - it gives an impression of bloat if you need to install a service to do that.

@winlonghorn: Now that's a weird way of finding the current directory...and what's with the camelCasing and the Pok?mon exception handling? (I know you didn't write it ; I'm just a bit OCD about that kind of stuff)

This topic is now closed to further replies.
  • Posts

    • My son is in Monaco right now, and I was checking his location in Apple's Find My app. I noticed that Prince Albert's Palace was blurred out on the satellite imagery in both Find My and Apple Maps. I checked Google Maps, and the palace wasn't blurred there. Does Apple have some kind of process where property owners can request that their homes be blurred on Apple Maps?  
    • No, it was THAT ugly and I’d rather forget it completely existed.
    • There is a lot of reasons not to use Edge but faster fixes and security updates is not one of them.
    • Can't reproduce. I installed Edge, went to neowin.net > accepted the cookie consent > used menu to go to forums, everything loads and I can browse around the forums. If you can't interact with the dialog on the forums for some reason, go to the main site and accept the cookie consent there? It is true that the site will not function properly until the cookie consent is accepted or rejected,. it's a legal requirement and I also know that certain VPN/ad blockers block it, which is a user related issue and not a neowin.net problem.   This is not our cookie consent dialog. Gotta love browser hijacking... /s Edit: this may be what Californians see, I will confirm with our consent provider.
    • Google Chrome 149.0.7827.115 (offline installer) by Razvan Serea The web browser is arguably the most important piece of software on your computer. You spend much of your time online inside a browser: when you search, chat, email, shop, bank, read the news, and watch videos online, you often do all this using a browser. Google Chrome is a browser that combines a minimal design with sophisticated technology to make the web faster, safer, and easier. Use one box for everything--type in the address bar and get suggestions for both search and Web pages. Thumbnails of your top sites let you access your favorite pages instantly with lightning speed from any new tab. Desktop shortcuts allow you to launch your favorite Web apps straight from your desktop. Chrome has many useful features built in, including automatic full-page translation and access to thousands of apps, extensions, and themes from the Chrome Web Store. Google Chrome is one of the best solutions for Internet browsing giving you high level of security, speed and great features. Important to know! The offline installer links do not include the automatic update feature. Download web installer: Google Chrome Web 32-bit | Google Chrome 64-bit | Freeware Download: Google Chrome Offline Installer 64-bit | Direct Link | 131.0 MB Download: Google Chrome Offline Installer 32-bit | Direct Link | 119.0 MB Download page: Google Chrome Portable Download: Chrome ARM64 | Direct Link View: Chrome Website | Release Notes Get alerted to all of our Software updates on Twitter at @NeowinSoftware
  • Recent Achievements

    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
    • Week One Done
      Timaximus earned a badge
      Week One Done
    • Rookie
      FBSPL went up a rank
      Rookie
    • First Post
      davidbazooked earned a badge
      First Post
  • Popular Contributors

    1. 1
      +primortal
      491
    2. 2
      PsYcHoKiLLa
      170
    3. 3
      +Edouard
      164
    4. 4
      Steven P.
      85
    5. 5
      ATLien_0
      76
  • Tell a friend

    Love Neowin? Tell a friend!