• 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.