0

我正在使用它来选择一个根文件夹并在将这些文件添加到我之前从每个目录中获取最新文件listbox,我想知道是否有办法知道当前目录以便在我仍在搜索文件时更新我的​​ UI。

var rootDirFile = Directory
                        .EnumerateFiles(pathToSearch, "*.pcap", SearchOption.TopDirectoryOnly)
                        .OrderByDescending(f => File.GetCreationTime(f))
                        .Take(1);

                    var allNewestFilesOfEachFolder = Directory
                        .EnumerateDirectories(pathToSearch, "*.*", SearchOption.AllDirectories)
                        .Select(d => Directory.EnumerateFiles(d, "*.pcap")
                            .OrderByDescending(f => File.GetCreationTime(f))
                            .FirstOrDefault());

foreach (string tempFile in rootDirFile.Concat(allNewestFilesOfEachFolder))
{
   //add the file
}
4

2 回答 2

0

可能最容易通过属性设置为的BackgroundWorker调用您的代码,然后处理事件WorkerSupportsProgresstrueReportProgress

于 2013-01-29T11:47:32.733 回答
0

我希望下面的代码有助于解决您的问题

// Observable collection is the best choice to bind to the UI elements it automatically refreshes the changes in the UT whenever the data modifies. 

    ObservableCollection<string> objList = new ObservableCollection<string>();

          //Bind this ObservableCollection to the UI Element with TwoWay binding

          var rootDirFile = Directory.EnumerateFiles(pathToSearch, "*.pcap", SearchOption.TopDirectoryOnly).OrderByDescending(f => File.GetCreationTime(f)).Take(1);
           // add to the observable collection

          var allNewestFilesOfEachFolder = Directory
                        .EnumerateDirectories(pathToSearch, "*.*", SearchOption.AllDirectories);

           // Instead of Iterating the Directory in a single time, seperate the task and iterate folder by folder basis.

            foreach (string obj  in allNewestFilesOfEachFolder )
            {
                var dir = Directory
                        .EnumerateFiles(obj, "*.pcap", SearchOption.TopDirectoryOnly)
                        .OrderByDescending(f => File.GetCreationTime(f))
                        .Take(1);    

                // add to the observable collection , it will automatically reflects the changes in the UI            

            }

如果您还需要绑定代码,请告诉我。我将简要解释

于 2013-01-29T13:36:18.750 回答