阅读从装配嵌入的文件

阅读从装配嵌入的文件

本文介绍了阅读从装配嵌入的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个配置文件的路径传递到框架方法(Gurok SmartInspect)。该配置文件是程序集的嵌入式资源。目前,我读了从组件文件,并将其存储外,然后通过路径名。有没有更好/更简单的方式来实现这一目标,而不复制该文件?



 私有静态无效ConfigLogger()
{
常量字符串embeddedFileName =xxx.SmartInspect.properties;
常量字符串configFileName =SmartInspect.properties;
ExtractFileFromAssembly(embeddedFileName,configFileName);
SiAuto.Si.LoadConfiguration(configFileName);
}

私有静态无效ExtractFileFromAssembly(字符串assemblyFileName,串configFileName)
{$ B $使用B(流S = Assembly.GetExecutingAssembly()。GetManifestResourceStream(assemblyFileName))
{
字节[]缓冲区=新的字节[s.Length]
INT读= s.Read(缓冲液,0,(int)的s.Length);
使用(的FileStream FS =新的FileStream(configFileName,FileMode.Create))
{
fs.Write(缓冲,0,buffer.Length);
}
}
}


解决方案

如果说Gurok SmartInspect读取配置信息的唯一方式是,你把它传递给一个路径的文件,你已经决定嵌入该文件在汇编,那么,你的方法是好的。你可能要考虑添加一些异常处理,但否则我看到这个没有问题。


HiI have to pass the path of a config file to a framework method (Gurok SmartInspect). The config file is an embedded resource of the assembly. Currently I read the file from the assembly and store it outside and then pass the pathName. Is there a better / less complicated way to achieve this goal, without copying the file?

    private static void ConfigLogger()
    {
        const string embeddedFileName = "xxx.SmartInspect.properties";
        const string configFileName = "SmartInspect.properties";
        ExtractFileFromAssembly(embeddedFileName, configFileName);
        SiAuto.Si.LoadConfiguration(configFileName);
    }

    private static void ExtractFileFromAssembly(string assemblyFileName, string configFileName)
    {
        using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(assemblyFileName) )
        {
            byte[] buffer = new byte[s.Length];
            int read = s.Read(buffer, 0, (int)s.Length);
            using (FileStream fs = new FileStream(configFileName, FileMode.Create))
            {
                fs.Write(buffer, 0, buffer.Length);
            }
        }
    }
解决方案

If the only way that Gurok SmartInspect reads configuration information is from a file that you pass it a path to and you've decided to embed that file in your assembly, then yes, your method is fine. You might want to consider adding some exception handling but otherwise I see no problem with this.

这篇关于阅读从装配嵌入的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 13:56