简体   繁体   中英

Cannot load resource no matter what I try

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM