从程序集中读取嵌入文件

发布于 2024-08-17 06:27:37 字数 953 浏览 1 评论 0原文

我必须将配置文件的路径传递给框架方法(Gurok SmartInspect)。配置文件是程序集的嵌入资源。目前,我从程序集中读取文件并将其存储在外部,然后传递路径名称。有没有更好/更简单的方法来实现这个目标,而不复制文件?

    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);
            }
        }
    }

I 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);
            }
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

云仙小弟 2024-08-24 06:27:37

如果 Gurok SmartInspect 读取配置信息的唯一方法是从您向其传递路径的文件,并且您决定将该文件嵌入到程序集中,那么是的,您的方法没问题。您可能需要考虑添加一些异常处理,但除此之外我认为这没有问题。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文