0

I am trying to read from a resource text file (words), using File.ReadAllLines(). But each time it runs i get the error message illegal characters in path. Any ideas?

//Get all of the words from the text file in the resources folder
string[] AllWords = File.ReadAllLines(Conundrum.Properties.Resources.Words);
John Saunders
  • 160,644
  • 26
  • 247
  • 397

4 Answers4

2

Try just referencing it directly, if it's actually added to the project as a resource:

var allWords = Conundrum.Properties.Resources.Words;

If you want it in an array, split on the newline character:

var allWords = Conundrum.Properties.Resources.Words
                 .Split(new[] { Environment.NewLine }, StringSplitOptions.None);

I just tried it out and that worked for me. Using File.ReadAllLines threw the same exception... I assume it actually wants a full file path leading to a file on disk.

Grant Winney
  • 65,241
  • 13
  • 115
  • 165
0

It's hard to diagnose this without knowing the value of the path you are trying to pass in. However, check out the C# functions Path.GetInvalidFileNameChars() and Path.GetInvalidPathChars(). With these, it should be easy to figure out what is wrong with your path!

var path = Conundrum.Properties.Resources.Words;
var invalidPathChars = path.Where(Path.GetInvalidPathChars().Contains).ToArray();
if (invalidPathChars.Length > 0)
{
    Console.WriteLine("invalid path chars: " + string.Join(string.Empty, invalidPathChars));
}

var fileName = Path.GetFileName(path);
var invalidFileNameChars = fileName .Where(Path.GetInvalidFileNameChars().Contains).ToArray();
if (invalidFileNameChars .Length > 0)
{
    Console.WriteLine("invalid file name chars: " + string.Join(string.Empty, invalidFileNameChars ));
}

As for resource files, not that these are typically compiled into the assembly and thus may not be available in raw form on disk. You can either retrieve them with builtin properties or with Assembly.GetManifestResourceStream().

ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
0

I have tried a few things out, this seems to work the best.

string resource_data = Properties.Resources.Words;
string[] AllWords = resource_data.Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries);
0

I know this is an old question but as I was lurking around these parts on this exact topic... here's my take:

From what I can see, the resource text file is being treated as a text file where it is hoped File.ReadAllLines(Conundrum.Properties.Resources.Words) would read the lines within the .txt file as strings.

However, by using ReadAllLines() this way, it immediately reads the first line as the filepath name, hence the error message.

I suggest adding the file as a resource as per this answer from @Contango.

Then, as per this answer from @dtb, with a little bit of modification, you will be able to read the text file contents:

        using System.Reflection; //Needs this namespace for StreamReader
        
        var assembly = Assembly.GetExecutingAssembly();
        
        //Ensure you include additional folder information before filename
        var resourceName = "Namespace.FolderName.txtfile.txt"; 

        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        using (StreamReader sr = new StreamReader(stream))
        {
            string line;

            while ((line = sr.ReadLine()) != null)
            {
                //As an example, I'm using a CSV textfile
                //So want to split my strings using commas
                string[] fields = line.Split(',');                  
                
                string item = fields[0];
                string item1 = fields[1];
            }
            sr.Close();
        }
Jimbob
  • 1