[英]C# Get full path of embedded ressource?
我正在使用.NET组件,该组件使用一种方法来读取特定的二进制文件,该方法期望具有完整路径名的字符串,如下所示:
Read("c:\\somefile.ext");
我把somefile.ext作为嵌入式资源放在我的项目中。 有什么办法可以将嵌入式资源的某种路径提供给组件的Read命令吗?
资源的路径是namespace.projectfolder.filename.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);
}
}
}
}
}
您需要使用项目的命名空间以及嵌入资源的名称。 例如,假设somefile.ext位于项目的文件夹资源/文件中,名称为ProjectA。 您应该用来读取嵌入资源的正确字符串是:
ProjectA.resources.files.somefile.ext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.