There are several points where the user can specify a what directory that data is to be saved to, I would like to be able to notify the user if they have chosen a directory that is being protected by Windows. I can just write a file to see if the OS lets me because UAC will write the file to a different directory.
-
possible duplicate of [How do you check for permissions to write to a directory or file?](http://stackoverflow.com/questions/130617/how-do-you-check-for-permissions-to-write-to-a-directory-or-file) – vcsjones Sep 23 '11 at 15:58
-
I don't think it's a duplicate. As OP noted in the question, some directories will silently redirect your file, instead of throwing an exception like that question is asking about. – Joe White Sep 23 '11 at 16:04
1 Answers
You can try to write a temporary file to the directory. If the directory is UAC protected and you're not running under admin privileges you will get a System.UnauthorizedAccessException error.
Edit: If @JoeWhite is correct and you are attempting to determine when your application is being affected by UAC Virtualization. I don't believe you can detect that in the application itself. The write calls will be adjusted by the OS automatically without any notice to your application.
You can read more on how UAC Virtualization works at via this article on the Windows Team Blog.
This only affects these specific cases though:
Your application writes to Program Files, Windows directories, or the system root (typically the C drive) folders
Your application writes to the Windows registry, specifically to HKLM/Software

- 1
- 1

- 6,031
- 1
- 20
- 21
-
1No -- as OP said in the question, some directories have redirection. If you try to write to your app's directory under Program Files, it will get silently written somewhere under Documents and Settings instead. – Joe White Sep 23 '11 at 15:55
-
@JoeWhite - So check for those cases, there are only a handful of those, and they are well documented. – Security Hound Sep 23 '11 at 15:57
-
2@Ramhound, if you know where they're documented, by all means, post an answer to the question! Of course, the trouble with checking for known exceptions is that the list may change in the next version of Windows. – Joe White Sep 23 '11 at 16:03
-
1That's not the way it works. Redirection is only enabled for non-Vista aware legacy programs. Not a C# app, it has a "hurt me if I don't do it right" manifest embedded. This answer is the most practical, given that there are plenty of other reasons the file can't be created. +1. – Hans Passant Sep 23 '11 at 16:04
-
@JoeWhite He shouldn't be writing code today that would be affected by UAC virtualization. The OS won't tell him he has been redirected. I'll edit the answer with a reference. – Frazell Thomas Sep 23 '11 at 16:06