C#Winform:将全局变量用于FileSystemWatcher路径

时间:2019-05-02 21:25:19

标签: c# winforms filesystemwatcher

我有一个带有两个FileSystemWatcher控件的表单。路径设置为通用,我们需要在路径不同的多个环境中进行部署。我在Program.cs中使用全局变量创建了一个类。

Program.cs

public static class Global {
    public const string connString = "<connection info>";
    public const string txtWatchPath = @"c:\test";
    public const string sumWatchPath = @"C:\Import";
}

我最初设置的观察者是这样的:

Form_Status.Designer.cs

// 
// txtWatcher
// 
this.txtWatcher.EnableRaisingEvents = true;
this.txtWatcher.Filter = "*_hdr.txt";
this.txtWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
this.txtWatcher.Path = Global.txtWatchPath;
this.txtWatcher.SynchronizingObject = this;
this.txtWatcher.Changed += new System.IO.FileSystemEventHandler(this.txtWatcher_Changed);
// 
// sumWatcher
// 
this.sumWatcher.EnableRaisingEvents = true;
this.sumWatcher.Filter = "*_hdr.sum";
this.sumWatcher.Path = Global.sumWatchPath;
this.sumWatcher.SynchronizingObject = this;
this.sumWatcher.Changed += new System.IO.FileSystemEventHandler(this.sumWatcher_Changed);

它最初可以工作,但是当我重新加载项目时,它们解析为路径而不是Global变量。

Form_Status.Designer.cs

// 
// txtWatcher
// 
this.txtWatcher.EnableRaisingEvents = true;
this.txtWatcher.Filter = "*_hdr.txt";
this.txtWatcher.NotifyFilter = System.IO.NotifyFilters.LastWrite;
this.txtWatcher.Path = "c:\\test";
this.txtWatcher.SynchronizingObject = this;
this.txtWatcher.Changed += new System.IO.FileSystemEventHandler(this.txtWatcher_Changed);
// 
// sumWatcher
// 
this.sumWatcher.EnableRaisingEvents = true;
this.sumWatcher.Filter = "*_hdr.sum";
this.sumWatcher.Path = "C:\\Import";
this.sumWatcher.SynchronizingObject = this;
this.sumWatcher.Changed += new System.IO.FileSystemEventHandler(this.sumWatcher_Changed);

当我在Designer中创建控件时,我只是将路径设置为“ C:\”,然后创建了Global变量,并设置了.Path = Global Variable。
但是,当我保存项目时,全局变量将解析为实际路径。这意味着当我在全局类上更改path变量时,它不会更改观察者的路径。

0 个答案:

没有答案