gpt4 book ai didi

c# - FileWatcher 典型实现?

转载 作者:太空宇宙 更新时间:2023-11-03 12:59:44 25 4
gpt4 key购买 nike

我正在构建一个应用程序,我将在其中查看文件。我对编程很陌生,所以我(也许)没有什么问题,因为我没有真正的实践。我的程序可以正常工作,但我不知道在“真实”软件中通常是如何实现的。请概述和评论。因为我喜欢在 DateFiles 类中尽可能多,而在 MainViewModel 中尽可能少。是的,应用程序基于 MVVM 构建。

我目前的状态:

  • 在我的 MainViewModel 中

    公共(public)静态字符串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");
    }
  • 数据文件类:

     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");
    }
    }

最佳答案

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

public class DataFiles : IDisposable {
public FileSystemWatcher filewatcher;
private readonly object fileListLock = new object();
// you don't need public setter on this
public ObservableCollection<Files> Fileslist { get; } = new ObservableCollection<Files>();

// pass path here, no need to use property
public void InitializeFiles(string path) {
// dispose existing watcher, if any
DisposeWatcher();
// Create a new FileSystemWatcher
filewatcher = new FileSystemWatcher();
// Set filter to only catch XAL files
filewatcher.Filter = "*.txt";
// Set the path
filewatcher.Path = path;
// 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);
// don't RefreshFilesList on UI thread, that might take some time and will block UI
Task.Run(() => RefreshFilesList());
// Enable the FileSystemWatcher events
filewatcher.EnableRaisingEvents = true;
}

private void FileOnchanged(object sender, FileSystemEventArgs e) {
// lock here to avoid race conditions with RefreshFilesList
lock (fileListLock) {
// better use dictionary to avoid looping over all files
// but looping is still much better than rebuilding whole list
var file = Fileslist.FirstOrDefault(c => String.Equals(c.FullPath, e.FullPath, StringComparison.OrdinalIgnoreCase));
if (file != null) {
if (e.ChangeType == WatcherChangeTypes.Deleted)
; // delete
else
; // update file properties
}
else {
// add new, unless event is delete
}
}
}

private void FileOnRenamed(object sender, RenamedEventArgs e) {
lock (fileListLock) {
// better use dictionary to avoid looping over all files
var file = Fileslist.FirstOrDefault(c => String.Equals(c.FullPath, e.OldFullPath, StringComparison.OrdinalIgnoreCase));
if (file != null) {
file.FullPath = e.FullPath;
}
else {
// add new
}
}
}

public void RefreshFilesList() {
// you need to lock here, because there is a race condition between this method and FileOnRenamed \ FileOnChanged,
// and you might lose some updates or get duplicates.
lock (fileListLock) {
// update ObservableCollection on UI thread
OnUIThreadDo(() => {
Fileslist.Clear();
});

DirectoryInfo dir = new DirectoryInfo(filewatcher.Path);
int nof = 0;

var files = new List<Files>();
// just use EnumerateFiles
foreach (FileInfo file in dir.EnumerateFiles("*.txt")) {
nof++;
int tmp = nof;
// if you are working with UI (that is most likely the case if you use ObservableCollection) -
// you need to update that collection from UI thread if you have bound controls
files.Add(new Files() {
FileId = tmp,
FullPath = file.FullName,
FileChanged = file.LastWriteTime,
FileCreated = file.CreationTime,
});
// don't do that
// NotifyPropertyChanged("fileslist");
}

// publish them all to collection on UI thread
OnUIThreadDo(() => {
foreach (var file in files)
Fileslist.Add(file);
});
}
}

private void OnUIThreadDo(Action a) {
if (Application.Current.CheckAccess())
a();
else
Application.Current.Dispatcher.BeginInvoke(a);
}

public void Dispose() {
DisposeWatcher();
}

private void DisposeWatcher() {
if (filewatcher != null) {
filewatcher.EnableRaisingEvents = false;
filewatcher.Created -= FileOnchanged;
filewatcher.Deleted -= FileOnchanged;
filewatcher.Changed -= FileOnchanged;
filewatcher.Renamed -= FileOnRenamed;
filewatcher.Dispose();
}
}
}

public class Files : INotifyPropertyChanged
{ // implement INotifyPropertyChanged, because you need to reflect property changes in UI
public int FileId { get; set; }
public string FullPath { get; set; }
public string FileName => Path.GetFileName(FullPath);
public DateTime FileChanged { get; set; }
public DateTime FileCreated { get; set; }
public string OnlyNameWithoutExtension => Path.GetFileNameWithoutExtension(FullPath);
}

关于c# - FileWatcher 典型实现?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32688376/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com