FileWatcher的典型实现?

时间:2015-09-21 06:08:26

标签: c# mvvm

我正在构建一个应用程序,我将在那里观看文件。我在编程方面很新鲜,所以我(也许)没有什么问题,因为我没有一些真正的实践。我的程序工作,但我不知道如何在“真正的”软件中实现。请概述和评论。因为我喜欢在DateFiles类中尽可能多地使用,而在MainViewModel中可能更少。 是基于MVVM基础构建的应用程序。

我的现状:

  • 在我的MainViewModel

    public static string ConfigurationFilesSourcePath2;

    private void InitializeFiles()
    {
        // New instance of DateFiles
        DF = new DataFiles();
    
        // Path to DateFiles
        DF.ConfigurationFilesSourcePath = ConfigurationFilesSourcePath;
    
        // Run Initialization method to establish "filewatching"
        DF.InitializeFiles();
    
        // Refresh ListView in View
        RefreshFileList();
    
        // Assign a Handler to PropertyChanged event
        DF.PropertyChanged += DF_PropertyChanged;
    }
    
    // If something change inside DateFiles
    private void DF_PropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        RefreshFileList();
    }
    
    // Refresh file list
    public void RefreshFileList()
    {
        fileslist = new ObservableCollection<Files>();
    
        foreach (var item in DF.fileslist)
        {
            fileslist.Add(item);
        }
    
        NotifyPropertyChanged("fileslist");
    }
    
  • DataFiles类:

     public class DataFiles : ViewModelBase
     {
    public FileSystemWatcher filewatcher;
    public string ConfigurationFilesSourcePath;
    public ObservableCollection<Files> fileslist { get; set; } = new ObservableCollection<Files>();
    
    public void InitializeFiles()
    {
        // Create a new FileSystemWatcher
        filewatcher = new FileSystemWatcher();
    
        // Set filter to only catch XAL files
        filewatcher.Filter = "*.txt";
    
        // Set the path
        filewatcher.Path = ConfigurationFilesSourcePath;
    
        // Subscribe to the Created event
        filewatcher.Created += new FileSystemEventHandler(FileOnchanged);
        filewatcher.Changed += new FileSystemEventHandler(FileOnchanged);
        filewatcher.Deleted += new FileSystemEventHandler(FileOnchanged);
        filewatcher.Renamed += new RenamedEventHandler(FileOnRenamed);
    
        // Enable the FileSystemWatcher events
        filewatcher.EnableRaisingEvents = true;
    
        RefreshFilesList();
    }
    
    private void FileOnchanged(object sender, FileSystemEventArgs e)
    {
        RefreshFilesList();
    }
    
    private void FileOnRenamed(object sender, RenamedEventArgs e)
    {
        RefreshFilesList();
    }
    
    public void RefreshFilesList()
    {
        fileslist.Clear();
    
        DirectoryInfo dir = new DirectoryInfo(ConfigurationFilesSourcePath);
    
        string[] extensions = new[] { ".txt" };
    
        int nof = 0;
        foreach (FileInfo file in dir.GetFiles().Where(f => extensions.Contains(f.Extension.ToLower())).ToArray())
        {
            nof++;
    
            fileslist.Add(new Files()
            {
                FileId = nof,
                FileName = file.Name,
                FileChanged = file.LastWriteTime.ToString(),
                FileCreated = file.CreationTime.ToString(),
                OnlyNameWithoutExtension = Path.GetFileNameWithoutExtension(file.Name)
            });
            NotifyPropertyChanged("fileslist");
        }
    }
    

1 个答案:

答案 0 :(得分:1)

您的代码存在一些问题。在最重要的部分中 - 您在每次更改时重建整个文件列表(而FileSystemEventArgs参数为您提供有关实际更改的内容和位置的信息),并且您不在UI线程上更新ObservableCollection。请查看以下代码(但请记住,这只是一个示例,供您发现一些问题):

a