• 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

    • eh I'll wait for the June 2026 MVS ISO downloads which should be coming out next Tuesday June 16 and possibly contain build 8655 instead of 8653
    • read this recent topic in another forum: https://www.askwoody.com/forums/topic/still-on-win-10-and-happy-to-be-there/ some people are happy sticking with Win10
    • Cooler Master MasterFrame 600 PC case is now 33% off on Amazon by Ivan Jenic The Cooler Master MasterFrame 600 is currently $109.99 on Amazon, down from its original $164.99 list price. That's 33% off and $55 saved on this premium aluminum mid-tower case with a modular design. If you're upgrading your PC case and want something that doesn't force you into a rigid layout, the MasterFrame 600 is worth a look. The case is built around the Cooler Master's FreeForm 2.0 platform, which lets you reconfigure the internal structure according to your hardware. Magnetic side panels allow for straightforward adjustments, and the case supports everything from Mini-ITX to E-ATX motherboards without compromise. There's also generous cooling headroom. Four pre-installed PWM fans handle airflow out of the box. GPU clearance goes up to 410mm, and the case supports radiators up to 420mm with room for three simultaneously. Truth be told, this might not be the prettiest case on the market, but it’s highly functional. The aluminum construction keeps the whole thing lightweight despite its size, and the finish looks noticeably better than the plastic mid-towers competing at this price point. If you want a serious, flexible case that prioritizes function over flashy aesthetics like RGB lighting, the MasterFrame 600 delivers at a reasonable price. Cooler Master MasterFrame 600 - $109.99 | 33% off on Amazon This Amazon deal is US-specific and not available in other regions unless specified. This is a first-party seller link (at the time of article publishing); ensure that you also purchase from a first-party seller link only. If you don't like it or want to look at more options, check out the previous deals that we have covered, OR you can also visit Amazon US deals page. Get Prime (SNAP), Prime Video, Audible Plus or Kindle / Music Unlimited. Free for 30 days. As an Amazon Associate, we earn from qualifying purchases.
    • DK, I don't use the extended channel, I'm always on the latest release.
  • Recent Achievements

    • Rookie
      restore went up a rank
      Rookie
    • Very Popular
      AndrewSteel earned a badge
      Very Popular
    • Veteran
      Taliseian went up a rank
      Veteran
    • One Month Later
      Clizby earned a badge
      One Month Later
    • One Month Later
      Timaximus earned a badge
      One Month Later
  • Popular Contributors

    1. 1
      +primortal
      509
    2. 2
      +Edouard
      162
    3. 3
      PsYcHoKiLLa
      155
    4. 4
      ATLien_0
      82
    5. 5
      Steven P.
      79
  • Tell a friend

    Love Neowin? Tell a friend!