[英]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: 笔记:
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());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.