Combine FileSystemWatcher better use profile

xiaoxiao2021-03-05  28

Everyone will use some assembled files during the development process to store configuration information for some applications. Although there is app.config under .NET WinForm, WebForm has web.config, but many times we create their own configuration files to store connection strings or store additional information.

Ok, cut into the topic! If you only use the configuration files provided by .NET, the only role below is to waste your time and hurt your eyes. How to use the configuration file is not the main content of this article. I assume that you have used your own profile, because the content changes in the configuration file are not frequent, in order to improve efficiency, during the program run, you will only read the information from the configuration file, then store it in a cache or one Single physical category. Of course, it is convenient and efficient. If the program is a tool software that is not long running, it is all beautiful and will not have any problems. But if the program is a long or simple, it is a background program, or a web program on a server. That pain began. Since configuration information only gets once, when your configuration information changes, the configuration information used in the program is still previously versions, and you have to manually restart the program to react the latest changes. If the program is on a server in the same place, there is only a vomiting blood.

The solution has N, below, the solution I use:

Use FileSystemWatcher! ! !

In my "" ""

A CONFIG.XML storage remote server has been used in the Automatic Update program, and every 20 seconds, the automatic update is connected to the URL recorded in the configuration file to get the update file. If you scan the remote server every time you scan the remote server Read the configuration file will not be wastable. I use a Config class to get configuration information. The Config class is a single-instance class, during the run, he has only one instance and has always existed. In the first time When he creates an instance and saves the configuration information to the object. After obtaining information from this object without reading the configuration file. Below is a simplified version of a CONFIG class:

Using

System;

Using

System.IO;

Using

System.xml;

Namespace

Unicom.utility

{/ ** ////

/// config summary description. /// Public class config {// Profile The path location of the profile Private const string file_path = @ "/ data / config.xml"; public static config _INSTANCE = NULL; // A config object PUBLIC STATIC CONFIG INSTANCE {GET {// First use, if there is no instance, create an if (_instance == null) {_instance = new config ();} return _instance;}} private string _url = string.empty; // Store URL information PUBLIC STRING URL {GET {Return_URL;}} private filesystemWatcher FileWatcher = NULL; // Read Configuration Information Private String GetInfofromFile () {// Start Read Profile, if configFilePath specifies a relative path, take the respective current path of the application, // if an absolute path, absolute path XmlTextReader myXml = new XmlTextReader (Path.Combine (System.AppDomain.CurrentDomain.BaseDirectory, FILE_PATH)); try {return myXml.ReadElementString ( "URL" );} catch (exception ex) {throw ex;} finally {myXml.close ();}} private config () {}} private config () {} = getInfofromFile (); // read and save configuration information in the XML file String FullPath = PATH .Combine (system.appdomain.currentdomain.baratedirectory, file_path); // Monitor the specified configuration file FileWatcher = New FileSystemWatcher Path.GetDirectoryName (FullPath), Path.GetFileName (FullPath)); fileWatcher.Changed = new FileSystemEventHandler (OnChange); fileWatcher.NotifyFilter = NotifyFilters.LastWrite; // last write time monitoring fileWatcher.EnableRaisingEvents = true; // allow trigger Event} // Profile When the configuration file changes, reread configuration information private void onchange () {_URL = getInfofromFile ();}}} This class will also have the problem I mentioned above, now we use the FileSystemWatcher class to solve, The description in the MSDN is as follows:

Use the FileSystemWatcher to monitor changes in the specified directory. You can monitor changes in files or subdirectories in the specified directory. This component monitors files on a local computer, a network drive or a remote computer. When the monitored file changes, FileSystemWatcher triggers the corresponding event, such as modification, creation.

FileSystemWatcher's following members are critical: Changed events,

Notifyfilter properties.

Notifyfilter is an enumerated member for monitoring the type of change, the member is as follows:

Member Name Description Value Attributes file or folder properties. 4CREATIONTIME file or creation time of the folder. 64DirectoryName directory name. 2FileName file name. 1 LastAccess file or date on the folder. 32lastwrite writes the date of the content to the file or folder last time. Security settings for the 16Security file or folder. 256Size file or folder size. 8

We only need LastWrite this one is enough.

Let

FileSystemWatcher informs us when file changes, and performs the specified code, we need to register the method to be executed to the change event of FileSystemWatcher, set other properties. Let's define a method to reread configuration information when the configuration file changes.

Now to modify a CONFIG class. First, add a member

FILESYSTEMWATCHER:

Private

FileSystemWatcher FileWatcher

=

NULL

;

Then, add the following code in the constructor.

//

Monitor the specified profile

String

Fullpath

=

Path.combine (system.appdomain.currentdomain.basedirectory, file_path); FileWatcher

=

New

FileSystemWatcher (Path.GetDirectoryName (Fullpath), Path.GetFileName (Fullpath); FileWatcher.Changed

=

New

FileSystem; FileWatcher.Notifyfilter

=

Notifyfilters.lastwrite;

//

Monitor last write time

FileWatcher.enableRaisingevents

=

True

;

//

Allow trigger events

Add a new method in the class: onchange to re-get the configuration information

//

Reread configuration information when configuring

Private

Void

ONCHANGE ()

{_Url = getInfofromfile ();}

Ok, everything goes, when config.xml changes, the latest configuration information will be loaded into memory in the first time. Pain is released.

Below is a complete CONFIG class after modification

Using

System;

Using

System.IO;

Using

System.xml;

Namespace

Unicom.utility

转载请注明原文地址:https://www.9cbs.com/read-33803.html

New Post(0)