gpt4 book ai didi

c# - 如何读取可移植类库中的资源文件?

转载 作者:IT王子 更新时间:2023-10-29 04:29:13 27 4
gpt4 key购买 nike

我有一个用于 Windows Phone 应用程序的可移植库。在同一个可移植图书馆中,我有几个内容文件 (Build Action = Content)。

我在 Portable Library 中创建了一个类 DataReader,它应该向我返回内容文件的流。但是,使用下面的代码,我始终从 GetManifestResourceStream 返回 null。我做错了什么?

public class DataReader
{
public static Stream GetStream(string code)
{
string path = string.Format("./data/code-{0}.dat", code);
return Assembly.GetExecutingAssembly().GetManifestResourceStream(path);
}
}

最佳答案

你的路径是错误的。您正在使用斜杠,但在嵌入式 list 资源名称中,斜杠在构建期间被转换为句点。此外,根据您的 PCL 目标平台,您甚至可能无法调用 Assembly.GetExecutingAssembly()

以下是您可以执行的操作:

var assembly = typeof(AnyTypeInYourAssembly).GetTypeInfo().Assembly;

// Use this help aid to figure out what the actual manifest resource name is.
string[] resources = assembly.GetManifestResourceNames();

// Once you figure out the name, pass it in as the argument here.
Stream stream = assembly.GetManifestResourceStream("Some.Path.AndFileName.Ext");

关于c# - 如何读取可移植类库中的资源文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10963781/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com