3

How do I get the MyDocuments folder of the currently-logged in standard user in a c# application running elevated with administrative privileges?

The code Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) returns the my documents folder of the administrator, but not the current user.

I'm testing on Windows 8, though an answer that works for all windows versions would be great

bboyle1234
  • 4,859
  • 2
  • 24
  • 29
  • 1
    This might possibly give you an idea on a start for how to do it: http://stackoverflow.com/questions/9873787/when-a-process-is-elevated-how-can-i-get-the-windows-authentication-id-of-the-no or this might be easier: http://stackoverflow.com/questions/4040290/get-the-logged-on-user-name-in-c-sharp – jordanhill123 Oct 15 '14 at 02:37
  • 1
    Running unelevated code, once elevated, is HARD. Perhaps you can make an unelevated wrapper that looks this up and passes it as a parameter to the elevated program? – Ben Voigt Oct 15 '14 at 02:59
  • Hey @ben-voigt, that's exactly what I ended up doing. It now bootstraps itself by starting non-elevated, then launching an elevated version of itself with those variables passed in as arguments – bboyle1234 Oct 15 '14 at 03:00
  • And now, how much do you trust those parameters, considering that they get used with admin rights? Do you trust the user not to spawn `"ElevatedWriteTheDataHere C:\Windows\System32"` and yell "hey admin, this task is waiting for your credentials!" – Ben Voigt Oct 15 '14 at 03:03
  • *grin* if they want to screw up their own system, they can go ahead – bboyle1234 Oct 15 '14 at 03:06
  • ah I get it, they can make it operate on someone else's user account ... well, they are admins anyway. – bboyle1234 Oct 15 '14 at 03:08
  • And one easy fix is to display the arguments on a screen for review, that asks whether to proceed Yes/No. – Ben Voigt Oct 15 '14 at 03:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/63072/discussion-between-bboyle1234-and-ben-voigt). – bboyle1234 Oct 15 '14 at 03:37

0 Answers0