[英]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.