如何在我的C#项目中包含文档?

时间:2012-03-22 14:37:32

标签: c#

好的,我有一个html文档,我在我的visual studio 2010 C#应用程序中引用,我需要该应用程序继续。

这是代码

string template = System.IO.File.ReadAllText("template.html");

template.html文件当前位于我的调试目录中,我希望能够在exe中包含它,因此用户不需要为我的应用程序运行html文件的副本。 ..关于如何解决这个问题的想法

5 个答案:

答案 0 :(得分:3)

右键单击解决方案资源管理器中的html文件 - >属性 - >将内容类型设置为Embedded Resource

更新: 您可以通过{ProjectBaseNameSpace} .Properties.Resources访问它。{ResourceName}

答案 1 :(得分:3)

您必须右键单击该文件并将其设为嵌入式资源。

enter image description here

然后您可以使用以下代码访问您的文件:

using (Stream stream = Assembly.GetExecutingAssembly()
                               .GetManifestResourceStream("template.html"))
using (StreamReader reader = new StreamReader(stream))
{
    string result = reader.ReadToEnd();
}

更多信息:http://msdn.microsoft.com/en-us/library/xc4235zt(v=vs.100).aspx

答案 2 :(得分:1)

将其设为Embedded Resource

答案 3 :(得分:0)

将您的文件包含在项目中,并在构建操作

下设置“嵌入式资源”

然后您可以访问该资源,以下是示例:

Assembly asm = System.Reflection.Assembly.GetExecutingAssembly();
string resourcePath = ".Templates.ActivationEmailTemplate.html";
Stream stream = asm.GetManifestResourceStream(asm.GetName().Name + resourcePath);

请注意,我的ActivationEmailTemplate.html位于“模板”文件夹

答案 4 :(得分:0)