简体   繁体   English

将目录路径及其中的文件存储为可观察的集合

[英]Storing directory paths with files inside those directories as observable collection

I have a program where I store directory inputs with files inside of that directory eg 我有一个程序,用于存储目录输入以及该目录中的文件,例如

C:/Documents
 C:/Documents/1.jpg
 C:/Documents/2.jpg

What would be the best data structure to store it so I could easily loop for just input directories or just by files or when I delete the directory the file would be delete as well. 什么是存储它的最佳数据结构,所以我可以轻松地循环访问仅输入目录或仅按文件,或者当我删除目录时,文件也会被删除。

I was thinking about using dictionary, but it has to also be observable. 我当时正在考虑使用字典,但是它也必须是可观察的。

What I use currently 我目前使用的是

private ObservableCollection<string> inputDirectories new ObservableCollection<string>();;
private ObservableCollection<string> inputFiles = new ObservableCollection<string>();

This solution doesn't really give me a lot of choices, I can't just get input episodes for certain input directory since they are in no way connected. 这个解决方案并没有给我很多选择,我不能仅仅获得某些输入目录的输入情节,因为它们之间没有任何联系。 When I delete inputDirectory I would have to loop and compare the input directory with my inputfiles to delete only files for that directory. 当我删除inputDirectory时,我将不得不循环并将输入目录与我的输入文件进行比较,以仅删除该目录的文件。

if you use the DirectoryInfo class from the System.IO namespace rather than a plain string, you can get the files included in this directory via the GetFiles method. 如果使用System.IO命名空间中的DirectoryInfo类而不是纯字符串,则可以通过GetFiles方法获取此目录中包含的文件。 You can then expose the list of all Files in a Property using LINQ. 然后,您可以使用LINQ公开属性中所有文件的列表。 Just make sure that you fire a OnPropertyChanged for this property whenever the inputDirectories change. 只要确保你开除一个OnPropertyChanged此属性每当inputDirectories变化。

private ObservableCollection<DirectoryInfo> inputDirectories = new ObservableCollection<DirectoryInfo>();

inputDirectories.Add(new DirectoryInfo(@"C:\Documents"));

public IEnumerable<string> Files => inputDirectories.SelectMany(d => GetFilesInDirectory(d));

private IEnumerable<string> GetFilesInDirectory(DirectoryInfo directory)
  {
     return directory.GetFiles().Select(x => x.FullName);
  }

I was thinking about using dictionary, but it has to also be observable. 我当时正在考虑使用字典,但是它也必须是可观察的。

There is no such class that implements both the IDictionary interface and the INotifyCollectionChanged interface available in the .NET Framework, ie there is no hybrid between a dictionary and an ObservableCollection<T> available, so you will have to create your own custom class. .NET Framework中没有实现IDictionary接口和INotifyCollectionChanged接口的类,即字典和ObservableCollection<T>之间没有混合,因此您将必须创建自己的自定义类。

You could take a look at the ObservableDictionary<TKey, TValue> class provided here: http://blogs.microsoft.co.il/shimmy/2010/12/26/observabledictionarylttkey-tvaluegt-c/ 您可以查看此处提供的ObservableDictionary<TKey, TValue>类: http : //blogs.microsoft.co.il/shimmy/2010/12/26/observabledictionarylttkey-tvaluegt-c/

An observable dictionary is indeed what you need. 可观察的词典确实是您所需要的。

You should store directory in Dictionary. 您应该将目录存储在Dictionary中。 Key should be directory and value should be inner directory or file. 键应该是目录,值应该是内部目录或文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 正确的方法来删除内部文件和目录,但不保留目录路径? - Proper way to delete files & directories inside but leave directory path alone? 查询可观察集合并将这些结果添加到列表框中? - query on observable collection and add those result to a list box? 可以观察到定期处理目录中的文件 - Observable to process files in a directory periodically 如何将目录中的文件夹名称加载到可观察的集合? - how to load folders names in a directory to an observable collection? Observable 集合没有被更新(类项的 observable 集合中的字符串的 observable 集合) - Observable Collection is not being Updated (observable collection of strings inside an observable collection of class item) WPF TreeView的C#可观察集合LDAP路径子级 - C# Observable Collection LDAP Paths Children for WPF TreeView 如何在可观察的收藏中获取价值? - How to get value inside observable collection? 如何在WPF中绑定到可观察集合内的列表 - How to Bind to a List inside an Observable Collection in WPF 目录获取目录访问被拒绝的异常和长路径的问题 - Issues with directory get directories access denied exceptions and long paths 关于文件路径和打开这些文件的问题 C# - Question about File Paths and opening those files C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM