0

I'd like to save an xml file to the MyDocuments folder on the local filesystem. To do so, I use the following:

xmlDoc.Save(Path.GetPathRoot(savePath + saveName));

Where savePath is

String qnPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "QuesitNote\\");
String savePath = Path.Combine(qnPath, "SavedDecks");

and saveName is a string inputted by the user, formatted to have no spaces.

I've been unable to do this because of issues with administrative rights. So far, I've tried a host of solutions, including the following:

The first method worked, but I only want admin rights for a particular method, the one in which I save the xml file. The second one gave me a System.Security.SecurityException. And the third appears to work, but does not actually create the file, only giving a "success" message I created. The UAC prompt never came up.

At this point, I have no idea how to fix this. Ideally, I would be able to use administrative rights for only one method (using my second attempt), but a fix for the third attempt would also be helpful. How would I get the UAC prompt to appear (third attempt), or otherwise fix my way of going about this?

Community
  • 1
  • 1
jmindel
  • 465
  • 1
  • 7
  • 16

1 Answers1

0

you can create a process and use the following properties:

Process.StartInfo.UseShellExecute = true;
Process.StartInfo.Verb = "runas";

and then in your process you can save your file.

amit dayama
  • 3,246
  • 2
  • 17
  • 28
  • Unfortunately, that doesn't work. I think I'll need to tweak other properties of the `ProcessStartInfo`. The UAC dialog still isn't showing up. – jmindel Nov 04 '15 at 04:52
  • And after trying to tweak properties and using Username and Password, with manual admin credential inputs using `SecureString`s, still no luck. – jmindel Nov 04 '15 at 05:33
  • if you want you can generate a messagebox which displays like UAC pop up? – amit dayama Nov 04 '15 at 07:16
  • Thing is, It's saving file with admin rights.. So when user click to save you can display a message and ask if you want to give admin privilages..and then your function with work accordingly – amit dayama Nov 04 '15 at 07:17
  • Yeah. I tried that, but I appreciate your solutions. But it repeatedly days that the username and password combination is incorrect. I even created another local account, but it says they're wrong. It may be troublesome that I'm on a virtual machine. – jmindel Nov 04 '15 at 07:49
  • Did u verify that local account has write access to that location – amit dayama Nov 04 '15 at 08:15
  • I think so. It's an administrator. I honestly am not even sure why I need administrative rights to access the file path; its just the documents folder. What's interesting is that using Environment.SpecialFolder winds up getting me the path to the documents folder on my host hard drive, not the virtual one. Just something to note. – jmindel Nov 04 '15 at 08:48