[英]C# Get full path of embedded ressource?
I'm using a .NET component that reads specific binary files using a method that expect a string with the full path name like this: 我正在使用.NET组件,该组件使用一种方法来读取特定的二进制文件,该方法期望具有完整路径名的字符串,如下所示:
Read("c:\\somefile.ext");
I've put the somefile.ext as an embedded resource in my project. 我把somefile.ext作为嵌入式资源放在我的项目中。 Is there any way I can feed some sort of path to the embedded resource to the component's Read command?
有什么办法可以将嵌入式资源的某种路径提供给组件的Read命令吗?
The path to the resource is of the form namespace.projectfolder.filename.ext . 资源的路径是namespace.projectfolder.filename.ext形式。
In order to read the contents, you can use a helper class like this one 为了阅读内容,您可以使用类似这样的帮助程序类
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);
}
}
}
}
}
You need to use the namespace of the project along with the name of the embedded resource. 您需要使用项目的命名空间以及嵌入资源的名称。 For example, assume somefile.ext is in the folder resources/files in your project with the name ProjectA.
例如,假设somefile.ext位于项目的文件夹资源/文件中,名称为ProjectA。 The the correct string you should use to read the embedded resource is:
您应该用来读取嵌入资源的正确字符串是:
ProjectA.resources.files.somefile.ext ProjectA.resources.files.somefile.ext
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.