0

I am trying to load a resource that I have added to my project, and it tells me:

Illegal characters in path.

Now, the name of the resource is: ShortcutList.txt. I don't see anything illegal about that. And the code I'm using is:

    public void InitShortcuts()
    {
        try
        {
            string s = File.ReadAllText(Properties.Resources.ShortcutList);
            if (!String.IsNullOrEmpty(s))
            {
                MessageBox.Show(s);
            }
        }
        catch (Exception exception)
        {
            MessageBox.Show(exception.Message);
        }
    }

But as I said above it just tells me that there are illegal characters in the path. How? It's not like I'm screwing up the path or anything.

I have set ShortcutList.txt as Embedded Resource and 'Copy if newer' (I've also tried every other option in that list!).

Any ideas what I might be doing wrong?

jay_t55
  • 11,362
  • 28
  • 103
  • 174
  • Does the ShortcutList contain any non-alphanumeric characters? – DJ Burb May 28 '13 at 15:31
  • 1
    If you look at the value of `Properties.Resources.ShortcutList` under the debugger, what does it say? – Mike Christensen May 28 '13 at 15:31
  • The resource isn't a file that is flushed out to the client's system when installed or executed, and therefore the resource doesn't turn into a path pointing to a resource on the physical file system. This will be the resource itself, so load into a `MemoryStream` then `FileStream` and so on. – Grant Thomas May 28 '13 at 15:31
  • The contents of the file contains letters, numbers, and symbols, such as [, ], ? and &. It will also include other normal symbols. – jay_t55 May 28 '13 at 15:33
  • possible duplicate of [How to read embedded resource text file](http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file) – Grant Thomas May 28 '13 at 15:33
  • So I take that the ShortcutList is a path. What's the underlying path for the ShortcutList, in this case? I think you may have a space or something in the path, that shouldn't be there – DJ Burb May 28 '13 at 15:35
  • Not a duplicate; the answer provided on that page did not help. – jay_t55 May 28 '13 at 15:35
  • ShortcutList is the filename. It is a text file. And I added it to Resources via the Designer. (It's in the Resources folder). – jay_t55 May 28 '13 at 15:36
  • Check out this http://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file stack overflow answer. I used it before when I had issues reading embedded resources. – Pete Garafano May 28 '13 at 15:31

1 Answers1

1

Just do something like this:

List<string> list = Resources.ShortcutList.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();

You can have your build action set to "Resource", and Copy to output to "Do not copy". It should work.

Janez Lukan
  • 1,439
  • 1
  • 12
  • 28