繁体   English   中英

如何计算文件夹中的资源

[英]How can i count Resources in a folder

我在项目中将多个图像设置为“ 资源” 现在我想将一个文件夹中的图像数量存储在一个变量中。

我怎样才能做到这一点? 我正在构建一个WPF应用程序。 当我尝试使用像这样的Pack URL时:

 string[] filePaths = Directory.GetFiles("pack://application:,,,/Resources/Images/Output/", "*.jpg");

我收到一个错误,指出不支持给定路径的格式。

笔记:

  • 在某些文件中未指定资源,它们只是在Build Action上设置为Resources。
  • 我只需要装配中的某些图像。 它们在特定的文件夹中

我试过的字符串:

  • 包://应用:,,, /资源/图片/输出/

  • 年鉴;组件/资源/图像/输出/

将其编写为普通的C#代码(使用Directory.GetFile() )并将其包装到T4模板中。

您无法计算资源,因此您必须计算将用作资源的文件。 这里是第一枪:

<#@ template language="C#" debug="true" hostSpecific="true" #>
<#@ output extension=".cs" #>
<#@ Assembly Name="System.Core.dll" #>
<#@ import namespace="System" #>
<#@ import namespace="System.IO" #>
<#
    var directory = Path.Combine(Path.GetDirectoryName(this.Host.TemplateFile), "Resources");
    var folderCounter = Directory.GetFiles(@"D:\", "*.*").Length;
#>

namespace MyNamespace
{
    public static class MyFilesCounter
    {
        public static int FilesInFolder = <#= folderCounter #>;
    }
}
int i = 0;
ResourceSet resourceSet = Properties.Resources.ResourceManager.GetResourceSet(CultureInfo.CurrentUICulture, true, true);
foreach (DictionaryEntry entry in resourceSet)
{
    string resourceName = entry.Key; //if you need all this, but who knows.
    object resource = entry.Value;

    if ((resource.GetType() == typeof(System.Drawing.Bitmap) || resource.GetType() == typeof(System.Drawing.Icon)) && 
         resourceName == "someString")
    {
        i++;
    }    
}

MessageBox.Show(i.ToString());

将您的投票重定向到这里 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM