3

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/

cadrell0
  • 17,109
  • 5
  • 51
  • 69
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142

2 Answers2

1

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 #>;
    }
}
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Excuse me, but how would i do that? i don't know what uri to use – eric.itzhak Feb 24 '12 at 14:26
  • But this is not from Resources it's from D:\. i need to deploy this to computers and need images saved in the assembly. – eric.itzhak Feb 24 '12 at 14:30
  • You can't count the resources, so you have to count the files the will be used as resources. I updated my answer that you get the folder where the T4 file resides. From there you can then adapt the path to the place you like. – Oliver Feb 24 '12 at 15:01
  • I seem to be missing some knowledge, never used a template before. i've tried writing the <#= folderCountrt #> in the page i need but it dosen't recognize the <> tags – eric.itzhak Feb 24 '12 at 15:11
  • 1
    Change the file extension of the file in your project to `.tt`, read [this article](http://www.hanselman.com/blog/T4TextTemplateTransformationToolkitCodeGenerationBestKeptVisualStudioSecret.aspx) and install [this Visual Studio Extension](http://visualstudiogallery.msdn.microsoft.com/40a887aa-f3be-40ec-a85d-37044b239591). – Oliver Feb 24 '12 at 15:15
  • Yep this worked, i didn't need to install the extention because i'm using Visual Studio 2010 i guess, Thank you very much for your help kind sir :) – eric.itzhak Feb 24 '12 at 15:34
  • @eric.itzhak: You don't need the extension to let it work in Visual Studio. But the extension gives you syntax highlighting and intellisense in your .tt files. – Oliver Feb 24 '12 at 16:12
0
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 :)

Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368
  • But this will count all my resource images, but i want to count only some. By the way i didn't mention i'm using WPF so i changed Bitmap to BitmapImage and it didn't count no items. – eric.itzhak Feb 24 '12 at 14:55
  • How do we know which images should be counted and which shouldn't be. Please mention your requirement clearly in the question. Give more if clauses before you increment the variable `i` in the code. I'll edit it – nawfal Feb 24 '12 at 15:00
  • I have used this successfully to get the number of image items in my resources using this code. Are you sure you have got image files added as resx files? – nawfal Feb 24 '12 at 15:19
  • no their not in resx files ( i've edited the question), their just images with build action set to Resources. i am using them in other parts of code and their working perfectly, so i know their in there – eric.itzhak Feb 24 '12 at 15:22
  • If they are not resx files, I think you are accessing from other parts by specifying the physical path. In that case give the proper path. I am not very competent in WPF aspects for that matter. – nawfal Feb 24 '12 at 15:28