GetManifestResourceNames为GetManifestResourceNames中具有已验证资源名称的嵌入资源返回null

时间:2017-07-24 20:35:28

标签: c# embedded-resource

我遇到GetManifestResourceStream为嵌入式资源返回null的情况,其中资源名称是针对GetManifestResourceNames进行验证的。

  • 许多国家/地区的嵌入式资源文件已添加到项目中。
  • 资源Build Action是“嵌入资源”。
  • 传递给GetManifestResourceStream的资源名称存在于GetManifestResourceNames返回的列表中。传递给GetManifestResourceStream的资源名称来自GetManifestResourceNames返回的列表。

我的步骤是:

  • 获取合格的资源路径。

    private string GetCountryResourcePath(string countryName)
    {
    Assembly assembly = this.GetType().GetTypeInfo().Assembly;
    string[] assemblyResources = assembly.GetManifestResourceNames();
    
    string searchTerm = string.Format(".{0}, countryName);
    
    foreach(string resourcePath in assemblyResources)
    {
        if (resourcePath.LastIndexOf(searchTerm, StringComparison.OrdinalIgnoreCase)>0)
            return resourcePath;
    }
    return string.Empty;
    }
    
  • 如果找到指定国家/地区的资源路径,请尝试使用GetManifestResourceStream打开:

    string countryResourcePath = GetCountryResourcePath( countryName );
    if (countryResourcePath == String.Empty)
        return;
    
    using (Stream countryStream = assembly.GetManifestResourceStream(countryStream))
    ...
    
  • 就我而言,使用countryName:“Russia.json”。

  • GetCountryResourcePath("Russia")返回“Jfx.Data.Countries.Russia.json”。

  • GetManifestResourceStream("Jfx.Data.Countries.Russia.json")返回null。

0 个答案:

没有答案