3

I'm working on a simple progam, and part of it populates a list from a txt file.

this probably is not a smart question but, I didn't find any info on this. I was wondering where is the best place to put text files in the application directory.

O know about the Resouces but, I wasn't able to get the path of the file I stored there.

so 2 questions:

  1. where is the best place to put a txt file? (or any other importent file to use in the application)

  2. if I put files in the Resources how do I get its path ?

(sorry fo my English)

Madushan
  • 6,977
  • 31
  • 79
samy
  • 1,949
  • 6
  • 39
  • 64
  • 1
    You don't need to be sorry about your English.. I've seen a lot worse! – Memet Olsen Aug 16 '12 at 23:29
  • 1
    I'll say in a comment that the best place for a text file is a directory you create within the value of %APPDATA% (roaming), %LOCALAPPDATA% or %PROGRAMDATA% (shared between useres) on Windows that has it, and within %USERPROFILE%. Since your only reason for asking is because you couldn't get resources working, then the answers here tackling that are what you really need. – Jon Hanna Aug 16 '12 at 23:36
  • And yeah, don't worry about your English. I'm an English-speaking writer and still write worse things in SO sometimes. – Jon Hanna Aug 16 '12 at 23:37

2 Answers2

3

If these are files that you do not need to expose to the users and only serve an internal purpose, then you can embed them in your assembly as resources, and extract them when you need them.

To do this, create a new directory in your application. Let's call it 'Resources'. Then, add text files to it. Use the properties window of each text file to change the BuildAction setting to "Embedded Resource". Then, in your code once you need the contents of the file you can use code like this to extract it:

using System.Reflection;

// ...

using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MyApplication.Resources.MyFile.txt")) {
    using (StreamReader reader = new StreamReader(stream)) {
        string contents = reader.ReadToEnd();
        // Do stuff with the text here
    }
}

If you don't want to do this, the correct location to place files is in a directory you create under the AppData directory. This is a known system path, which you can obtain like this:

string folderLocation = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
string myPath = Path.Combine(folderLocation, "MyAppName");

You can then use a StreamReader or other class in System.IO to find/enumerate and read the files.

kprobst
  • 16,165
  • 5
  • 32
  • 53
1

When an application has associated/companion data files it sometimes makes sense to embed them as a Resource, because then there is less chance for them to be tampered with e.g. deleted, or the data modified.

And other times it makes sense to keep the file loose....so you have to decide the best place to store them....you can locate these in the place where the application is installed, or in the Application Data/AppData directory.


For embedding files in Resources have a look at this link:

It has a step-by-step guide showing how to embed a file (e.g. a Text file into Resources), and then using a StreamReader to access it and read its contents.


To store the files and access them from a suitably located directory you can use:

System.Environment.SpecialFolder.ApplicationData

with

Environment.GetFolderPath()

to find out where the AppData directory is.

Then when you create your application Setup/Installer, you should get it to create a directory for your application underneath AppData, and then you can decide what files you want to be installed into that location.

See:

Note, ApplicationData "roams"...i.e. when you logon to a different machine, the files are transferred onto that machine as part of your profile....you may not want this....so you could instead use:

System.Environment.SpecialFolder.CommonApplicationData
Community
  • 1
  • 1
Colin Smith
  • 12,375
  • 4
  • 39
  • 47