Joshua12
Joshua12

Reputation: 21

Convert A Text File From Resources Into Array

I need to read a txt file I uploaded in Resources folder and then I have to convert it into an a string[] array. Anyway, when I debug the code, at the second line I get the following exception: System.ArgumentException: 'Illegal characters in path'.

I've tried this method with a file from my computer and it works. I checked my text for special characters but I havent'found anything. I removed space at the beginning of a line.

          string text = Convert.ToString(Resources.File);
          string[] lines = File.ReadAllLines(text).ToArray();

Try with file from computer: it works.

        string text = Convert.ToString(@"C: \File.txt");
        if (File.Exists(text))
        {
            string[] lines = File.ReadAllLines(text);
            int i = 1;

            if (lines[i] == abc.Text)
            {
                DO STUFF;
                f2.ShowDialog();
            }
        }

I except the file to be converted.

Upvotes: 1

Views: 251

Answers (1)

Reza Aghaei
Reza Aghaei

Reputation: 125227

When you add a .txt file to a .resx resource, the designer will create a string property for that.

Assuming the property name is MyFile, then to get the lines you just need to split the string using Environment.NewLine:

var lines = Properties.Resources.MyFile
    .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

Note: If for any reason, you are interested to have the byte[] for a .txt file, you can set the file type in resource designer to binary:

enter image description here

Upvotes: 2

Related Questions