Start New Discussion within our Software Development Community

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.

Comments
Good snippet.
using System;
using System.Diagnostics;
using System.IO;

namespace Whittle {
    static class ApplicationStorage {
        static private String processName = Process.GetCurrentProcess().ProcessName;

        static public String ApplicationData {
            get {
                return MakeDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData));
            }
        }

        static public String LocalApplicationData {
            get {
                return MakeDirectory(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData));
            }
        }

        static public String CommonApplicationData {
            get {
                return MakeDirectory(Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData));
            }
        }

        static private String MakeDirectory(String p) {
            String path = Path.Combine(p, processName);
            Directory.CreateDirectory(path);

            return path;
        }
    }
}

I knew where they were supposed to go but didn't know how to find the correct path. Nice info.

The article starter has earned a lot of community kudos, and such articles offer a bounty for quality replies.