Welcome Guest! To access all forums & features, please register an account or sign-in. → Why register?



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


6 replies to this topic - - - - -

#1 sathenzar

    Resident Fanatic

  • 872 posts
  • Joined: 12-June 06

Posted 26 April 2012 - 18:33

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.


#2 virtorio

    1076

  • 6,935 posts
  • Joined: 28-April 03
  • Location: New Zealand
  • OS: OSX 10.8, Windows 8
  • Phone: Windows Phone 7.8

Posted 26 April 2012 - 18:39

In this case I think having the program run elevated all the time is appropriate.

#3 winlonghorn

    Resident Elite

  • 1,037 posts
  • Joined: 17-March 05
  • Location: Erie, PA

Posted 26 April 2012 - 18:45

View Postsathenzar, on 26 April 2012 - 18:33, said:

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;
	    }


#4 Orry Verducci

    Neowinian²

  • 178 posts
  • Joined: 20-December 05
  • Location: Cambridge, UK

Posted 26 April 2012 - 20:30

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.

#5 Kami-

    ♫ d(-_-)b ♫

  • 3,625 posts
  • Joined: 28-July 08
  • Location: SandBox

Posted 27 April 2012 - 11:40

View PostOrry Verducci, on 26 April 2012 - 20:30, said:

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.

#6 greenwizard88

    Resident Elite

  • 1,224 posts
  • Joined: 28-November 04

Posted 27 April 2012 - 11:43

Here's another vote for running it as a service.

#7 Aethec

    Neowinian Senior

  • 2,214 posts
  • Joined: 02-May 10

Posted 27 April 2012 - 21:12

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)