ShoTo
ShoTo

Reputation: 163

Reading resource txt line by line

Had a txt file on my desktop with code:

string source = @"C:\Users\Myname\Desktop\file.txt"
string searchfor = *criteria person enters*
foreach (string content in File.ReadLines(source))
{
if (content.StartsWith(searchfor)
{
*do stuff*
}
}

I recently just learned I can add the txt as a resource file (as it will never be changed). However, I cannot get the program to read that file.txt as a resource line by line like above. I have tried Assembly.GetExecutingAssembly().GetManifestResourceStream("WindowsFormsApplication.file.txt")

with a stream reader but it says invalid types.

Basic concept: user enters data, turned into a string, compared to the starting line of the file.txt as it reads down the list.

Any help?

edit Jon, I tried as a test to see if it is even reading the file:

        var assm = Assembly.GetExecutingAssembly();
        using (var stream = assm.GetManifestResourceStream("WindowsFormsApplication.file.txt")) ;
        {
            using (var reader = new StreamReader(stream))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    label1.Text = line;
                }
            }
        }

It says "The name stream does not exist in the current context" and "Possible Mistaken Empty Statement" for the stream = assm.Get line

Upvotes: 0

Views: 1470

Answers (2)

ShoTo
ShoTo

Reputation: 163

Found the issue:

The file, while loaded as a resource, despite all the tutorials saying it is NameSpace.File, the truth is the system puts the location as NameSpace.Resources.File, so I had to update that as well.

Then I used the following code:

        string searchfor = textBox1.Text
        Assembly assm = Assembly.GetExecutingAssembly();
        using (Stream datastream = assm.GetManifestResourceStream("WindowsFormsApplication2.Resources.file1.txt"))
        using (StreamReader reader = new StreamReader(datastream))
        {
            string lines;
            while ((lines = reader.ReadLine()) != null)
            {
                if (lines.StartsWith(searchfor))
                {
                    label1.Text = "Found";
                    break;
                }
                else
                {
                    label1.Text = "Not found";
                }
            }
        }

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1499940

You can use a TextReader to read a line at a time - and StreamReader is a TextReader which reads from a stream. So:

var assm = Assembly.GetExecutingAssembly();
using (var stream = assm.GetManifestResourceStream("WindowsFormsApplication.file.txt"))
{
    using (var reader = new StreamReader(stream))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            ...
        }
    }
}

You could write an extension method on TextReader to read all the lines, but the above is simpler if you only need this once.

Upvotes: 2

Related Questions