的Visual Studio 2012 - C#:读取“的.txt”文件从资源 [英] Visual Studio 2012 - C#: Reading a '.txt' File From Resources

查看:431
本文介绍了的Visual Studio 2012 - C#:读取“的.txt”文件从资源的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访问和读取从我的资源,一个文本文件,achInfo.txt在Visual Studio中。一些解决方法已经被列在这个网站上,但他们都不为我工作。他们只给我两个错误,我将解释以后之一。

I am trying to access and read a text file, 'achInfo.txt' from my resources in Visual Studio. Some workarounds have already been listed on this site, but none of them seem to work for me. They just give me one of two errors, which I will explain later on.

下面是整个方法为止。

private string[] GetAchMetadata(short element)
    {
        string[] temp = { "", "" };
        string cLine;
        try
        {
            StreamReader sr = new StreamReader(Properties.Resources.achInfo);
                while (!sr.EndOfStream)
                {
                    cLine = sr.ReadLine();
                    if (Microsoft.VisualBasic.Information.IsNumeric(cLine))
                    {
                        if (int.Parse(cLine) == element)
                        {
                            temp[0] = cLine;
                            temp[1] = sr.ReadLine();
                            return temp;
                        }
                    }
                }

        }
        catch (Exception e)
        {
            System.Diagnostics.Debug.WriteLine("There was a problem in collecting data.");
            System.Diagnostics.Debug.Write(e);
        }

        return temp;
    }



我的第一个假设是使用 Properties.Resources。 achInfo ,因为这直接涉及到有问题的文件。然而,这将引发 System.ArgumentException 错误,用描述'路径中具有非法字符

My first assumption was to use Properties.Resources.achInfo, as this refers directly to the file in question. However, this throws a System.ArgumentException error, with the description 'Illegal characters in path'.

然后我使用的组装方案('Grandma_Lair'是我的空间,不要问):

I then used the assembly solution ('Grandma_Lair' being my namespace, don't ask.') :

Assembly asm;
asm = Assembly.GetExecutingAssembly();
StreamReader sr = new StreamReader(asm.GetManifestResourceStream("Grandma_Lair.achInfo.txt"));



不过,这将引发一个 System.ArgumentNullException 与消息值不能为空。
我还设置了访问修饰符我经济上的公众,而我已经确定将文件设置为嵌入的资源。

But, this throws a System.ArgumentNullExceptionwith the message 'Value cannot be null'. I have also set the access modifier on my resources to public, and I have made sure the file is set to an Embedded Resource.

有没有人有任何我的问题的想法?

Does anyone have any idea on my problem?

推荐答案

您第一个解决方案应该在您更换工作的StreamReader StringReader

Your first solution should work once you replace StreamReader with StringReader:

StringReader sr = new StringReader(Properties.Resources.achInfo);



Properties.Resources.achInfo 表示您在给你作为一个字符串,而不是到资源的路径(因此路径无效字符错误)全部嵌入的资源。

The value Properties.Resources.achInfo represents the resource that you embedded in its entirety given to you as a string, not a path to that resource (hence the "invalid characters in the path" error).

GetManifestResourceStream 办法应太,但你需要给方法正确的道路,这是基于,除其他事项外,你的项目的默认命名空间的名称。如果您添加到 assembly.GetManifestResourceNames()的调用试图让你的资源,寻找你的资源名称在调试器中准确拼写之前,你应该能够解决空指针异常问题也是如此。

The GetManifestResourceStream approach should work too, but you need to give the method the right path, which is based, among other things, on the name of the default namespace of your project. If you add a call to assembly.GetManifestResourceNames() before trying to get your resource, and look for the exact spelling of your resource name in the debugger, you should be able to fix the null pointer exception issue as well.

这篇关于的Visual Studio 2012 - C#:读取“的.txt”文件从资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆