Friday, 22 April 2011

AppData in .NET service

Found an interesting .NET issue today.

if(!Directory.Exists(@"%AppData%\Aether")) Directory.Create(@"%AppData%\Aether");
File.WriteAllText(@"%AppData%\Aether\AetherService.log", "test");

This should result in:
[vista/7] C:\Users\Username\AppData\Roaming\Aether\AetherService.log
[winxp/2k] C:\Documents and Settings\Username\Application Data\Aether\AetherService.log

Since the service is running as SYSTEM, %AppData% actually turns out to be blank. It therefore creates a directory called %AppData% in C:\Windows\System32, with the contents being created under that. A quick workaround is to read the name of the logged on user manually and then create the path yourself.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.