簡體   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