0

I have an application that can be installed anywhere. I don't want it to prompt the user when the program is in an unprotected folder. Is it possible to only show a UAC prompt when the application is in Program Files (or a similar protected folder)?

I'm using this code at the moment to check if it has write access (from this question):

public static bool HasWriteAccess(string folder) {
    try {
        System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folder);
        return true;
    }
    catch(UnauthorizedAccessException) {
        return false;
    }
}

I know that the code cannot be run in Program.cs or frmMain_Load because that is after the application has started, and so won't work.

Community
  • 1
  • 1
Abluescarab
  • 537
  • 8
  • 25
  • A New process is required – Joe DF Feb 27 '14 at 05:11
  • @JoeDF Yes, but how do I do that? Should I make some kind of console application that acts as the main executable and then check with the above code? – Abluescarab Feb 27 '14 at 05:12
  • You can try this approach: 1. start installer. 2. get user input on where to install, other options. 3. check if you can install in that folder without UAC prompt, if yes, do that. 4. If no, restart your installer with parameters which contain all the user input, but this time start it elevated, UAC prompt will be shown and your installer knows it should just continue where the unelevated installer exited. – Lev Feb 27 '14 at 08:24
  • @Lev I just realized I wasn't clear enough. The application is *already installed* in that location. I'm talking about the application itself, rather than the installer. Thank you, though. – Abluescarab Feb 27 '14 at 10:36
  • @Abluescarab oh well ok :) You could try the same mechanism :) Start your app, check if UAC is needed, if yes, restart elevated! – Lev Feb 27 '14 at 10:39
  • @Lev That's a good thought. I was just looking for the easiest method for the user. Thanks again. – Abluescarab Feb 27 '14 at 10:43
  • If you put your check before showing the form, the user will not notice anything. When the you don't need UAC you don't restart your app of course. – Lev Feb 27 '14 at 10:44
  • @Lev Perfect. I'll see about doing that. Thanks! – Abluescarab Feb 27 '14 at 10:49
  • @Lev Sorry, I've been really busy lately. I haven't been able to try your solution yet. I hope someone has found this helpful, at least! – Abluescarab Mar 07 '14 at 02:30

1 Answers1

1

I just wrote a small test to see if the idea works. This works:

    static void Main(string[] args)
    {
        string _name = Assembly.GetExecutingAssembly().Location + Guid.NewGuid().ToString();
        try
        {
            File.CreateText(_name).Close();
            File.Delete(_name);
        }
        catch (UnauthorizedAccessException)
        {
            Restart();
            return;
        }

        Console.WriteLine("execution with write access");
        Console.ReadKey();
    }

    private static void Restart()
    {
        ProcessStartInfo startInfo = new ProcessStartInfo();
        startInfo.FileName = Assembly.GetExecutingAssembly().Location;
        startInfo.Verb = "runas";
        Process p = Process.Start(startInfo);
    }
Lev
  • 434
  • 2
  • 9