c# - 通过从dll中获取文件来读取文件的内容

标签 c# dll

我有一个名为(“测试”)的 C# 项目,它有一个类和一个包含 html 文件的文件夹,我的上级已将这个项目编译成一个 dll。

html文件的内容是= "Hello World"

该类包含:

读取整个 html 文件的字符串。 context.Respone.Write(上面的字符串)。

我有另一个 web 项目,它有一个页面可以通过添加测试 dll 来调用上面的方法。问题是,如何通过从 dll 中获取 html 文件的内容来读取它?使网页可以显示“Hello World”

最佳答案

将您的文件放入嵌入式资源并使用这样的代码读取它

    public static string GetResourceFileContentAsString(string fileName)
    {
        var assembly = Assembly.GetExecutingAssembly();
        var resourceName = "Your.Namespace." + fileName;

        string resource = null;
        using (Stream stream = assembly.GetManifestResourceStream(resourceName))
        {
            using (StreamReader reader = new StreamReader(stream))
            {
                resource = reader.ReadToEnd();
            }
        }
        return resource;
    }

关于c# - 通过从dll中获取文件来读取文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27835301/

相关文章:

c# - 将 ListViewItem 的字符串复制到 TextBox 中

c# - 如何在不实际遍历所有图片像素的情况下使 C# 中的图片变亮?

c# - 如何创建多线程 Dll

使用 MatLab dll : one or more multiply defined symbols found 时出现 C++ 错误

visual-c++ - 从 Visual C++ DLL 导出非托管类?

c# - C# API 错误和异常处理的最佳实践

javascript - 函数错误(){ [ native 代码]}

c++ - 查找与 C++ dll 中的方法一起使用的参数

c# - 如何使用PCM 8kHZ和每个样本16位的Naudio loopbackcapture独占模式

c# - 将 C# Forms 应用程序注入(inject)另一个应用程序