I have this application that need to do some things in protected paths (like %PROGRAMFILES%), I know that I should be using %APPDATA%, but I can't change that for now. I have isolated all the things that could require UAC to show up on another project, here's a sample code:
using System;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
class Class1
{
static void Main(string[] args)
{
try
{
File.CreateText(Path.Combine(Application.StartupPath, "something.txt"));
}
catch (UnauthorizedAccessException ex)
{
MessageBox.Show(ex.Message, "UnauthorizedAccessException", MessageBoxButtons.OK, MessageBoxIcon.Error);
if (args.Length == 0)
{
Process proc = new Process();
proc.StartInfo.FileName = Application.ExecutablePath;
proc.StartInfo.Arguments = "not again";
proc.StartInfo.Verb = "runas";
proc.Start();
}
else
{
MessageBox.Show("Exit to avoid loop.");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Exception", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
So, I call this executable from my main program, and if it fails because of an unauthorized access, it will launch itself showing the UAC request.
My questions are:
1) I had to convert the project output from a DLL to an EXE because I couldn't find any way to request UAC elevation from a DLL, is there any easy way to do that?
2) I also noticed that some programs show a personalized UAC message, with the program logo and all those things, let me show you an example:
How can I do that for my program?
3) To avoid entering in a loop when is running with elevated privileges an it gets another UnauthorizedAccessException I did that thing passing any args. What would you do to achieve the same goal?
I think that's all for now. Thanks for your time.