Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否有更简单的语法来访问程序集中嵌入的资源?_C#_.net_.net 4.0 - Fatal编程技术网

C# 是否有更简单的语法来访问程序集中嵌入的资源?

C# 是否有更简单的语法来访问程序集中嵌入的资源?,c#,.net,.net-4.0,C#,.net,.net 4.0,现在,我在一个班里有几个: string upgrade_file = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream( Assembly.GetExecutingAssembly().GetName().Name + ".schemas.template.db_upgrades.txt") ).ReadToEnd(); 我可以编写一

现在,我在一个班里有几个:

string upgrade_file 
    = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(
        Assembly.GetExecutingAssembly().GetName().Name 
        + ".schemas.template.db_upgrades.txt")
        ).ReadToEnd();
我可以编写一个包装器函数来简化对嵌入式资源的访问(如果必须的话,我也会这样做),但是有没有一种更简单或更优雅的方法可以通过.Net本机访问它们呢

例如,我发现GetManifestResourceStream(…)不是静态的很奇怪。另一个例子:对于文本文件,是否存在返回字符串而不是流的方法

更新1

要明确的是,我在子目录中有文本文件,我希望:

  • 这些文件仍然是单独的文件(例如,为了能够对它们进行单独的源代码控制)
  • 这些文件仍然是嵌入式资源,即使用程序集编译
  • 现在我正在这样做,如图所示:

    更新2

    我没有设法使用答案中的任何内容来简化文件的访问

    以下是我现在使用的帮助器方法,以防其他人发现它有用:

    /// <summary>
    /// Get the content of a text file embedded in the enclosing assembly.
    /// </summary>
    /// <param name="resource">The "path" to the text file in the format 
    /// (e.g. subdir1.subdir2.thefile.txt)</param>
    /// <returns>The file content</returns>
    static string GetEmbeddedTextFile(string resource)
    {
        return new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(
            Assembly.GetExecutingAssembly().GetName().Name + "." + resource)).ReadToEnd();
    }
    
    //
    ///获取嵌入在封闭程序集中的文本文件的内容。
    /// 
    ///格式中文本文件的“路径”
    ///(例如subdir1.subdir2.thefile.txt)
    ///文件内容
    静态字符串GetEmbeddedTextFile(字符串资源)
    {
    返回新的StreamReader(Assembly.GetExecutionGassembly().GetManifestResourceStream(
    Assembly.getExecutionGassembly().GetName().Name+““+resource”).ReadToEnd();
    }
    
    我的项目中有一个名为
    Resource1
    的资源文件,其中包含一个TXT文件和一个二进制文件。
    我能做到

    string str = Resource1.TxtFile;
    byte[] file = Resource1.BinaryFile;
    

    您知道吗,在VS中,如果您有一个Resources.resx文件,它会生成一个真实类的代码?您可以查看它并直接使用它,而无需任何元操作


    如果双击Rsources.resx文件并打开设计器,您可以找到一个下拉列表,该下拉列表将生成的类的访问权限从内部更改为公共(如果出于您的目的需要)。

    我个人的方法是为Assembly类编写扩展,因为这似乎是一个无论如何都应该包含在该类中的方法

    因此,如上所述,首先确保您的文本文件标记为“嵌入式资源”,然后使用类似以下代码:

    public static class Extensions
    {
        public static string ReadTextResource(this Assembly asm, string resName)
        {
            string text;
            using (Stream strm = asm.GetManifestResourceStream(resName))
            {
                using (StreamReader sr = new StreamReader(strm))
                {
                    text = sr.ReadToEnd();
                }
            }
            return text;
        }
    }
    
            string content = Assembly.GetExecutingAssembly().ReadTextResource(myResourceName);
    
    这允许您使用以下代码从DLL或任何程序集加载它:

    public static class Extensions
    {
        public static string ReadTextResource(this Assembly asm, string resName)
        {
            string text;
            using (Stream strm = asm.GetManifestResourceStream(resName))
            {
                using (StreamReader sr = new StreamReader(strm))
                {
                    text = sr.ReadToEnd();
                }
            }
            return text;
        }
    }
    
            string content = Assembly.GetExecutingAssembly().ReadTextResource(myResourceName);
    

    (上面的代码可以更简洁,但我使用它只是为了演示)

    你可以看看这个类。关于资源,我猜EF对XML映射也有同样的作用,根据连接字符串的外观-我想知道那里是否有反射宝石…您能完全限定资源吗?我没有
    资源。*
    可用,所以我想知道
    资源
    属于哪个命名空间或类型。我想这应该是Properties.ResourcesCorrect,%namespace%。属性。Resources@sixfeetsix,若要以这种方式访问资源,必须通过项目属性中的“资源”选项卡添加资源。出于实际原因,我希望保留单独文件中描述的资源,主要是它允许我分别对它们进行源代码控制。@sixfeetsix:我不确定我编辑的答案是否是您要搜索的答案,无论如何,看一看无论如何,我可以做一些类似的事情,并将我的每个资源保存在单独的文件中?我只是尝试使用资源,并意识到:1-添加一个文件资源将文件放入资源目录,然后跟踪其更改;2-资源仍嵌入在.dll文件中;这正是我想要的。