我的自定义服务器控件项目中目前有一个文件“abc.htm”,Build Action
设置为Embedded Resource
。
现在在RenderContents(HtmlTextWriter output)
方法中,我需要阅读该文件并在网站上进行渲染。
我正在尝试以下但是无济于事:
protected override void RenderContents(HtmlTextWriter output)
{
var providersURL = Page.ClientScript.GetWebResourceUrl(typeof (OpenIDSel), "OpenIDSelector.Providers.htm");
var fi = new FileInfo(providersURL); // <- exception here
//the remaining code is to possibly render the file
}
这是providersURL
的示例:
/WebResource.axd?d=kyU2OiYu6lwshLH4pRUCUmG-pzI4xDC1ii9u032IPWwUzMsFzFHzL3veInwslz8Y0&t=634056587753507131
FileInfo
投掷System.ArgumentException
:路径中存在非法字符。
答案 0 :(得分:1)
您可以执行以下操作:
protected override void RenderContents(HtmlTextWriter output)
{
var source = ReadEmbeddedResource("AssemblyName", "OpenIDSelector.Providers.htm");
//the remaining code is to possibly render the file
}
private string ReadEmbeddedResource(string assemblyName, string resouceName)
{
var assembly = Assembly.Load(assemblyName);
using (var stream = assembly.GetManifestResourceStream(resouceName))
using(var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}