2009-06-04 110 views
0

我有一个html模板,我想从VS 2005 C#窗体表单应用程序中的资源文件中检索。如何从C#中的资源文件中检索文本文件VS.2005

我在项目中创建了一个名为/ html /的文件夹,该文件夹中有一个名为template.html的文件。

我已将该文件添加到我的资源中。我将它的名称看作模板,它的文件路径是完全限定的文件名(c:/.../project/html/import.html)。它被保存为TEXT而不是二进制。

我试过很多方法来提取这个文件,但每次我得到一个空返回。我错过了什么?

 Type t = GetType(); 
     Assembly a = Assembly.GetAssembly(t); 
     string file = "html.template.html"; // I've tried template and template.html 
     string resourceName = String.Concat(t.Namespace, ".", file); 

     Stream str = a.GetManifestResourceStream(resourceName); 

     if (str == null) // It fails here - str is always null. 
     { 
      throw new FileLoadException("Unrecoverable error. Template could not be found"); 
     } 
     StreamReader sr = new StreamReader(str); 
     htmlTemplate = sr.ReadToEnd(); 

回答

1

您是否尝试过在你的输出装配在寻找Reflector验证资源名称实际上是你希望它是什么呢?

0

我相信当你试图检索它时,你会误导你的资源。

你可以做的一件事是用Reflector检查生成的程序集并检查资源的全名。

1

反射器帮助找出问题所在,谢谢。这是我需要的:

string template = Properties.Resources.template; 

真的不是那么容易。以上所有其他的东西是完全没有必要的。