简体   繁体   中英

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. When i try and use Pack URL like this :

 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 .
  • 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.

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 :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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