简体   繁体   English

如何计算文件夹中的资源

[英]How can i count Resources in a folder

I Have several images set as Resources in my project. 我在项目中将多个图像设置为“ 资源” now i want to store in a variable the amount of images i have in that folder. 现在我想将一个文件夹中的图像数量存储在一个变量中。

How can i achive this? 我怎样才能做到这一点? I Am building a WPF Application. 我正在构建一个WPF应用程序。 When i try and use Pack URL like this : 当我尝试使用像这样的Pack URL时:

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

i get an error that The given path's format is not supported. 我收到一个错误,指出不支持给定路径的格式。

Notes: 笔记:

  • The resources are not specified in some file, they are just set as Resources on it's Build Action . 在某些文件中未指定资源,它们只是在Build Action上设置为Resources。
  • I only need some of the images in the assembly. 我只需要装配中的某些图像。 They are within a specific folder 它们在特定的文件夹中

Strings i tried: 我试过的字符串:

  • pack://application:,,,/Resources/Images/Output/ 包://应用:,,, /资源/图片/输出/

  • YearBook;component/Resources/Images/Output/ 年鉴;组件/资源/图像/输出/

Write it as normal C# code (using Directory.GetFile() ) and wrap it into a T4 Template. 将其编写为普通的C#代码(使用Directory.GetFile() )并将其包装到T4模板中。

You can't count the resources, so you have to count the files the will be used as resources. 您无法计算资源,因此您必须计算将用作资源的文件。 Here a first shot: 这里是第一枪:

<#@ 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());

Redirect your votes to here :) 将您的投票重定向到这里 :)

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

相关问题 如何播放放置在Resources文件夹中的SWF文件 - How can I play a SWF file placed into Resources folder 如何在资源文件夹中使用json? - How can I use json inside my resources folder? 如何访问Xamarin.Forms中Resources(iOS)文件夹的文件路径? - How can I access the filepath to Resources (iOS) folder in Xamarin.Forms? 在Visual Studio中,如何从项目Resources文件夹中加载.png图像? - In Visual Studio how can I load a .png image from the project Resources folder? 如何在资源中添加程序集? - How can I add assemblies inside resources? 如何更改Resources.resw? - How can I change the Resources.resw? 我可以将 windows (.bat ) 文件放在 Visual Studio C# 项目的资源文件夹中吗? - Can I put a windows (.bat ) file inside the resources folder of a Visual studio C# project? 如何将文件夹(带有帮助)添加到资源以及如何使用它 - How to add a folder (with help) to resources and how to work with it C#:如何获取Resources文件夹中的所有资源及其名称 - C#: How to get all Resources in Resources folder and their names CodeGo.net&gt;如何分配资源文件夹的字符串 - c# - How to assign resources folder to a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM