Emerica.
Emerica.

Reputation: 568

Reading Numbers From Text

Ok so I have the following code:

string numbers = File.ReadAllText("numbers.txt");
            StringSplitOptions.RemoveEmptyEntries);
            List<string> List = new List<string>();
            List.AddRange(allNumbers);

            return List;

Currently the numbers.txt file is in a directory on my machine, but I want the text file to be integrated into my solution in a file such as numbers/numbers.txt. How do I read from that file instead of reading it from the default file VS2010 likes to read from? I already have a file in my solution called numbers with the respective file in it.

Upvotes: 0

Views: 178

Answers (3)

SouthShoreAK
SouthShoreAK

Reputation: 4296

To usurp a little bit of BrokenGlass's code, you can also do this

string fileName = Server.MapPath("numbers/numbers.txt");
string numbers = File.ReadAllText(fileName);

Server.MapPath converts a relative path on your site into a file path on the hosting machine.

Upvotes: 0

Dumbo
Dumbo

Reputation: 14102

You can add the file as a resource to your project and use it easily later. Here is more info: http://msdn.microsoft.com/en-us/library/7k989cfy(v=vs.90).aspx

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160852

Something like this:

string outputDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
string fileName = Path.Combine(outputDirectory, "numbers.txt");
string numbers = File.ReadAllText(fileName);

Make sure you set the "Copy to output directory" property to "Copy if newer" for the file in question in your project.

Upvotes: 2

Related Questions