我正在使用一个 .NET 组件,该组件使用一种需要具有完整路径名的字符串的方法来读取特定的二进制文件,如下所示:
Read("c:\\somefile.ext");
我已将 somefile.ext 作为嵌入式资源放入我的项目中。有什么方法可以将某种嵌入资源的路径提供给组件的读取命令吗?
资源路径的形式为命名空间.项目文件夹.文件名.ext.
为了阅读内容,您可以使用像这样的辅助类
public class ResourceReader
{
// to read the file as a Stream
public static Stream GetResourceStream(string resourceName)
{
Assembly assembly = Assembly.GetExecutingAssembly();
Stream resourceStream = assembly.GetManifestResourceStream(resourceName);
return resourceStream;
}
// to save the resource to a file
public static void CreateFileFromResource(string resourceName, string path)
{
Stream resourceStream = GetResourceStream(resourceName);
if (resourceStream != null)
{
using (Stream input = resourceStream)
{
using (Stream output = File.Create(path))
{
input.CopyTo(output);
}
}
}
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)