C # Winform: использование глобальной переменной для пути FileSystemWatcher - PullRequest
0 голосов
/ 03 мая 2019

У меня есть форма с двумя элементами управления 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);

Первоначально это работает, но когда я перезагружаю проект, они разрешают путь вместо глобальной переменной.

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: \», затем я создал глобальные переменные и установил .Path = Global Variable.
Однако, когда я сохраняю проект, глобальная переменная преобразуется в фактический путь. Это означает, что когда я изменяю переменную пути в глобальном классе, он не изменит путь для наблюдателя.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...