资源 (resx) 查找返回不返回内容



我创建了一个需要嵌入外部文本文件的 .net 核心类库。我已经将 Resource.resx 添加到项目根目录,并将我的文本文件添加到 resx。我现在可以通过代码访问文本文件

var a = Resource.MyTxtResourceFile

智能感知让我知道MyTxtResourceFile"查找类似于 [文件的正确内容] 的本地化字符串

当我运行代码时Resource.MyTxtResourceFile实际返回字符串

directory1directory2mytxtresourcefile.txt;System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089;utf-8

而不是文本文件的内容。

Resource.Designer 看起来像这样

/// <summary>
///    Looks up a localized string similar to [the contents of my text file]
/// </summary>
public static string MyTxtResourceFile {
get {
return ResourceManager.GetString("MyTxtResourceFile ", resourceCulture);
}
}

谁能帮忙?

我不再直接向resx文件添加内容。我只是在项目中包含文本文件并使用GetManifestResourceStream方法来获取内容,然后使用StreamReader将流转换为字符串。

例如,我有一个名为Stamina.World.DataAccess.SqlClient的项目。子文件夹中有一个名为ReadAll.sql的文件AuthenticationAttemptScript.我想获取该文件的内容。这是代码

var manifestResourceName = "Stamina.World.DataAccess.SqlClient.AuthenticationAttempt.Script.ReadAll.sql";
var contents = string.Empty;
// this assumes the file is in the current assembly
var currentAssembly = this.GetType().Assembly;
using (var manifestResourceStream = currentAssembly.GetManifestResourceStream(manifestResourceName))
{
if (manifestResourceStream == null)
{
// TODO : handle this
}
using (var streamReader = new System.IO.StreamReader(manifestResourceStream))
{
contents = streamReader.ReadToEnd();
}
}
// do something with the file contents

最新更新