eric.itzhak
eric.itzhak

Reputation: 16072

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:

Strings i tried:

Upvotes: 3

Views: 2508

Answers (2)

Oliver
Oliver

Reputation: 45119

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 #>;
    }
}

Upvotes: 1

nawfal
nawfal

Reputation: 73253

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

Upvotes: 0

Related Questions