views:

114

answers:

5

If you have a Windows application that needs to write working files as part of its normal operation (not prompted by the user):

  • Where on the file system should they be written?
  • What's the correct way to get this directory in .NET for all Windows versions (XP/V/7/Server)?

(Writing to the same directory as the executable or a "temp" directory are not suitable for my specific case.)

+2  A: 

Environment.SpecialFolder.LocalApplicationData would be the .NET way of doing it.

Myles
+5  A: 

The most common place would be:

string path = Environment.GetFolderPath(
      Environment.SpecialFolder.ApplicationData);

But there are other values of Environment.SpecialFolder that could be useful. And of course there is the User's Documents folder.

And you say you don't want a Temp file but if you need it in 1 session,

 string path = System.IO.Path.GetTempFileName();

can be very useful.

Henk Holterman
+3  A: 

This has been asked and answered before on Stackoverflow. Check the following two questions:

My winform app uses xml files to store data, where should I store them so Vista users can write to them?

Windows Standard File Locations

Franci Penov
+1  A: 

Yes, under AppData (usually %APPDATA%\[company name]\[application name] -- the location of %APPDATA% is different between XP and Vista/7, so be sure to use an abstraction layer and don't hardcode the path) is the standard location. Please, please, please, please don't put them anywhere under Program Files.

Max Strini
+5  A: 

Another option is Isolated Storage (Intro.) It may be used per assembly, per machine, per user, and any combination of above.

Matthew Whited