tags:

views:

72

answers:

1

In my application, if the user saves a file to a folder they don't have permissions for, File.Copy will fail. An example is saving a document to the C:\ root.

Instead of denying access, I'd like to prompt the user to elevate permissions with a UAC prompt, but only for this save function (not for the entire application). Is there a way to do this?

+2  A: 

In short... no.

The entire process needs to be elevated and that elevation needs to happen at startup. However! You can create a separate process to do this work. Make a separate .exe that does only this work and gets everything it needs in the command line parameters. You can add verbs to the process that will cause it to be elevated:

Process p = new Process();
p.StartInfo.FileName = "copy.exe";
p.StartInfo.Arguments = new [] { pathFrom, pathTo };
p.Verb = "runas";
p.Start();

Something like that...

justin.m.chase
I am not a fan of runas in general - I prefer to have the exe that is launched have a manifest, and tell Process to use ShellExecute. However I give you a valid point here because you're using copy, which obviously shouldn't be manifested.You could even put this in the code that notices the first copy failed. Nice.
Kate Gregory