I have a C# application that is being installed for all users.
Every user on the pc can use the program, and I need to store user-specific Data per user.
I started with Enviroment.SpecialFolder.ApplicationData
giving me C:\Users\USER\AppData\Roaming
.
But when users choose run as admin and enter admin credentials, then this won't work, it will give me:
C:\Users\ADMIN\AppData\Roaming
So I started using a WMI call:
ManagementObjectSearcher searcher = new ManagementObjectSearcher("SELECT UserName FROM Win32_ComputerSystem");
ManagementObjectCollection collection = searcher.Get();
string username_with_domain = (string)collection.Cast<ManagementBaseObject> ().First().ToString();
and get the folder by the username.
But: this won't work in a Remote Desktop Session (RDP / MSTSC) - it returns "" so I have to fallback to Enviroment.UserName
.
Summary
- User starts .exe: Works. (ENVIROMENT.UserName)
- User uses "run as Administrator" with "ADMIN" user: Works. (WMI Call)
- User starts .exe in a RDP session: Works. (fallback to (ENVIROMENT.UserName))
- User starts .exe in a RDP session with "run as Administrator" with "ADMIN" user: does not work
Question: How can I get the AppData folder or Username of the current logged in User? e.g. who owns the Desktop I see?