seattleite7
seattleite7

Reputation: 350

File in project I/O at runtime

If I have a file, like a .txt file (not a Resource or Content file) in my project that I want to read/write to at runtime, in a debugging session from Visual Studio, what's the simplest way to do that?

Visual Studio has to hold references to these files' locations in order to load them, right? I want to do the same thing.

I'm planning to have hundreds of these files and don't want to set everything to "Content" and "Copy if Newer" in Visual Studio.

Upvotes: 0

Views: 127

Answers (1)

Typist
Typist

Reputation: 1474

Put all your text files under a folder and read them with following code -

using System.IO;

foreach (string file in Directory.EnumerateFiles(folderPath, "*.txt"))
{
    string contents = File.ReadAllText(file);
}
  • .Net Framework 4.0 - EnumerateFiles
  • .Net Framework Previous versions - GetFiles

Upvotes: 1

Related Questions