SaulBack
SaulBack

Reputation: 1416

Find All User's AppData Directories

My project is a plugin to a much larger project. A bug in the larger project creates a file that will cause my application to crash. They are already aware of the issue and are addressing it, but in the meantime the only work around is to delete a certain file in the appdata directory for each user.

The plan is that during installation of the smaller plugin, to navigate to every user's appdata directory to delete a problematic file (if it exists). Is there any way to find each User's appdata directory. Remember this is an install, so we will have admin privileges. Some of the things I have seen is to use

WindowsIdentity.Impersonate(IntPtr someUser)
Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)

Then it would just be a matter of somehow getting the IntPtr, which I am unsure how to get.

That is the best solution I have right now. So how do I get the IntPtr of each user? Or is there a better solution?

The next best that I can think of would simply be to go through all directories of C:\Users\eachUser\AppData\ and deleting the file as it exists. But then again, in XP that location would have to be C:\Documents and Settings\eachUser\Application Data\ and I would be creating operating system specific code. Suggestions?

Upvotes: 2

Views: 1730

Answers (1)

agent-j
agent-j

Reputation: 27923

From WindowsIdentity.Impersonate Method (IntPtr userToken):

IntPtr userToken is...

The handle of a Windows account token. This token is usually retrieved through a call to unmanaged code, such as a call to the Win32 API LogonUser function.

So, you can't do it this way unless you have all the usernames and passwords.

Edit: Why not delete the file at runtime for just the current user? Does your plugin have permission to do this?

Edit 2: You can use the installing user's directory from Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), and then use Directory.GetDirectories to find the other users that have a profile. Then, you can tack on the special folder name. Pretty round-about, but it might work in your "pinch".

Upvotes: 4

Related Questions