When Microsoft was developing Windows Vista (and they continued this in Windows 7) they decided to add a security feature to the Program Files directories. This feature is that all the directories are read only. The idea behind this is to prevent malicious software from overwriting your exe/dll files. This, of course, brings up the problem: Where do I put my application specific files that are created at runtime. This tutorial will answer that question!
One thing you will notice with Windows is that there are a lot of special folders (My Documents, Desktop, My Music, My Pictures, etc.) and these directories aren't available through the Path and Directory classes (They are if you know exactly where to look for them, but the location is configurable by user thus making them difficult to find). But the locations are available through the GetFolderPath method of the System.Enviroment.Class:
String myDocuments = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
There are a lot of different values in the Environment.SpecialFolder enum, but for this tutorial we are only concerned with ApplicationData
, LocalApplicationData
, and CommonApplicationData
.
ApplicationData is where you store files that you wish available to the user that travel with them (if roaming profiles are enabled). LocalApplicationData is for non-roaming data and CommonApplicationData is for data available to all users of the machine.
The snippet is a simple class that will return to you the path to any of these directories. It also appends the current process name so all files created by your application will be in one directory (and help prevent name conflicts). If the directory doesn't exist it will create it.