Click here to Skip to main content
15,397,959 members
Articles / Programming Languages / C#
Posted 20 Apr 2012

Tagged as


41 bookmarked

Where Should I Store My Data?

Rate me:
Please Sign up or sign in to vote.
5.00/5 (33 votes)
20 Apr 2012CPOL2 min read
There is a common theme in development: storing data files in the application folder. This is bad, and wrong, and should be stamped on!
In this tip, you will see how to store data files and avoid problems arising from choosing the wrong location. You will also see how to get the path.


Recently, I have been answering questions regarding storing data files, and the problems that result from choosing the wrong location. So, where should you store data? And how do you get the path?


Windows has a number of places that are free for data storage and which will not cause "Access Denied" exceptions when you try to write to them - unfortunately, the default directory is not one of them. If you use code such as:

File.WriteAllText("myFile.txt", "Hello World"); 

Then the system will try to write it to the current folder - which is normally the application executable directory, which in release programs is (or should be) under the "Program Files" or "Program Files(x86)" folder. It is not a good idea to write anything here - you may get an exception, or the user may not be able to locate it with Windows Explorer, or worse: it may well not be on a regular backup schedule folder list.

If you want to write data to a sensible location, Windows provides three Data folders:

User Data
Roaming User Data
All User Data

And these are accessible in C# via the Environment.SpecialFolder enumeration:


However, it is not a good idea to use these directly, as they are not specific to your application, but general to all.

One way to get an appropriate folder is just to use the Application Properties:


But I am not in favour of this as the path is specific to your application, and the version number of the assembly: If you issue a new version of your EXE, then it may not be able to find the data that worked with a previous version.

Instead, I prefer to use the Application GUID and the Environment.SpecialFolder enumeration.

When you create an assembly with Visual Studio, it is assigned a Guid value, which does not change during the life of that assembly - be it an EXE file or a Class Library. You can use that Guid to absolutely identify your application, and thus it's data - there is a very, very small chance of any two applications being given the same GUID - which is not the case for application names! How many companies do you think have wanted to call an application "Word", or "Paint"?

The table at the bottom shows the problem with using the Application Properties too blindly.

The Code

Include the following in your application data storage class, or in an appropriate Class Library.

/// <summary>
/// Get the Application Guid
/// </summary>
public static Guid AppGuid
        Assembly asm = Assembly.GetEntryAssembly();
        object[] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true));
        return new Guid((attr[0] as GuidAttribute).Value);
/// <summary>
/// Get the current assembly Guid.
/// <remarks>
/// Note that the Assembly Guid is not necessarily the same as the
/// Application Guid - if this code is in a DLL, the Assembly Guid
/// will be the Guid for the DLL, not the active EXE file.
/// </remarks>
/// </summary>
public static Guid AssemblyGuid
        Assembly asm = Assembly.GetExecutingAssembly();
        object[] attr = (asm.GetCustomAttributes(typeof(GuidAttribute), true));
        return new Guid((attr[0] as GuidAttribute).Value);
/// <summary>
/// Get the current user data folder
/// </summary>
public static string UserDataFolder
        Guid appGuid = AppGuid;
        string folderBase = Environment.GetFolderPath
        string dir = string.Format(@"{0}\{1}\", folderBase, appGuid.ToString("B").ToUpper());
        return CheckDir(dir);
/// <summary>
/// Get the current user roaming data folder
/// </summary>
public static string UserRoamingDataFolder
        Guid appGuid = AppGuid;
        string folderBase = Environment.GetFolderPath
        string dir = string.Format(@"{0}\{1}\", 
                     folderBase, appGuid.ToString("B").ToUpper());
        return CheckDir(dir);
/// <summary>
/// Get all users data folder
/// </summary>
public static string AllUsersDataFolder
        Guid appGuid = AppGuid;
        string folderBase = Environment.GetFolderPath
        string dir = string.Format(@"{0}\{1}\", 
                     folderBase, appGuid.ToString("B").ToUpper());
        return CheckDir(dir);
/// <summary>
/// Check the specified folder, and create if it doesn't exist.
/// </summary>
/// <param name="dir"></param>
/// <returns></returns>
private static string CheckDir(string dir)
    if (!Directory.Exists(dir))
    return dir;

Do note that if you include this code in a Class Library, then AppGuid will not be the same as the AssemblyGuid - a Class Library is a separate Assembly, and has its own Guid. This can be useful, if you want to store information which is to be used by multiple applications - User Login details for example if these are not related to the Windows Login details.

Sample Paths Generated

Common.AppGuid                     158aa00d-332e-440b-9c2c-47e2fc11c078
Common.AssemblyGuid                8d14ff7e-8d39-4163-a1ba-4fae4a5c361d
Common.UserDataFolder              C:\Users\griff\AppData\Local\
Application.UserAppDataPath        C:\Users\griff\AppData\Roaming\SetShares\SetShares\
Common.UserRoamingDataFolder       C:\Users\griff\AppData\Roaming\
Application.LocalUserAppDataPath   C:\Users\griff\AppData\Local\SetShares\SetShares\
Common.AllUsersDataFolder          C:\ProgramData\{158AA00D-332E-440B-9C2C-47E2FC11C078}\
Application.CommonAppDataPath      C:\ProgramData\SetShares\SetShares\


  • 20th April, 2012: Original version


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

QuestionCalculator hide application Pin
Rajput Poonam20-Apr-21 8:21
MemberRajput Poonam20-Apr-21 8:21 
BugAppGuid property limitation... Pin
cchester13-Apr-20 6:23
Membercchester13-Apr-20 6:23 
Questionmy vote of 5 Pin
Michael Haephrati22-Oct-17 2:13
mvaMichael Haephrati22-Oct-17 2:13 
QuestionMy vote of 5 Pin
Bob Stoom24-Jan-16 1:07
professionalBob Stoom24-Jan-16 1:07 
QuestionSuperb! Pin
Mark@Metro23-Mar-15 17:56
MemberMark@Metro23-Mar-15 17:56 
Question[My vote of 2] Static gettters Pin
Истинското Ми-Име26-Jun-14 12:46
MemberИстинското Ми-Име26-Jun-14 12:46 
AnswerRe: [My vote of 2] Static gettters PinPopular
PIEBALDconsult26-Jun-14 12:55
professionalPIEBALDconsult26-Jun-14 12:55 
SuggestionChecking Directory exists Pin
PabloInNz10-Jun-14 18:07
MemberPabloInNz10-Jun-14 18:07 
GeneralNice work ! Pin
PabloInNz10-Jun-14 16:27
MemberPabloInNz10-Jun-14 16:27 
GeneralMy vote of 5 Pin
Maciej Los3-May-14 3:18
mveMaciej Los3-May-14 3:18 
Questionabout the guid Pin
User349011-Jun-13 8:25
MemberUser349011-Jun-13 8:25 
AnswerRe: about the guid Pin
OriginalGriff11-Jun-13 8:34
mveOriginalGriff11-Jun-13 8:34 
GeneralRe: about the guid Pin
User349011-Jun-13 8:42
MemberUser349011-Jun-13 8:42 
GeneralMy vote of 5 Pin
Menon Santosh11-Mar-13 2:05
professionalMenon Santosh11-Mar-13 2:05 
QuestionSaving in registry Pin
Thomas Daniels19-Jan-13 4:54
mentorThomas Daniels19-Jan-13 4:54 
AnswerRe: Saving in registry Pin
PIEBALDconsult19-Jan-13 5:11
professionalPIEBALDconsult19-Jan-13 5:11 
QuestionWhat about parent of LocalUserAppDataPath Pin
LesF18-Aug-12 16:01
MemberLesF18-Aug-12 16:01 
AnswerRe: What about parent of LocalUserAppDataPath Pin
OriginalGriff18-Aug-12 20:12
mveOriginalGriff18-Aug-12 20:12 
QuestionOk..but Pin
FatCatProgrammer21-Apr-12 4:16
MemberFatCatProgrammer21-Apr-12 4:16 
GeneralGood Tip Pin
perilbrain20-Apr-12 10:08
Memberperilbrain20-Apr-12 10:08 
GeneralRe: Good Tip Pin
OriginalGriff20-Apr-12 21:43
mveOriginalGriff20-Apr-12 21:43 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.