Customize profile in .NET

zhaozj2021-02-16  184

table of Contents

Summary Profile Structure Read Configuration Management Configuration Summary

Summary

When developing a project using Microsoft.NET, there may be a Windows application, web application, Web Service, Windows Service and other applications. If you want these applications to use the same configuration (such as a database connection), do not want to repeat different profiles. Then .NET provides the profile scenario that may not meet your purpose. This article describes a simple configuration file using an XML format and its usage. This article assumes that your project has at least one Windows application, a web application and a Window Service application.

Profile structure

In order to enable all applications to access the configuration file, this instance puts the configuration file in the WinNT / System32 system directory. Of course, the reader can define the location where the file is stored, but requires attention to the portability of the program. The Windows System Directory can be obtained using the Windows API function, but the Windows, Web, and Window Service applications requested are required to be read on the system directory.

For convenience, we named this profile to System.config (same as Microsoft .NET's profile extension), if you do not specify a configuration file name in the program, the configuration file defaults to system.config.

The structure of the configuration file is as follows, the reader can add the configuration file according to its own needs.

localhost

SA

Pubs

True

20

3

300

UID = "sa"

PWD = ""

Database = "northwind"

Pooling = "True"

MaxPoolsize = "20"

MINPOOLSIZE = "3"

Lifetime = "300"

/>

Description: It can be seen that there are two forms, and the first configuration value is written directly on the XML node and the other writes the configuration value on the property of the node. The acquisition and settings of these two node configurations will be described separately in the following sections.

The configuration file uses an XML structure. About XML grammar and concepts, there are many related materials on the network, please readers to refer to the network resources. The first node saves the database configuration using the child node to obtain the configuration of the database by getting the child node value. The second node uses the property of the node to save the database configuration. Read configuration

Here's how to read the System.config configuration file using the program.

1. Auxiliary procedure: The following block uses the Windows API function to get the system directory.

Using system.Runtime.InteropServices;

Using system.text;

[DLLIMPORT ("kernel32")]]

Private static extern void getSystemDirectory (StringBuilder sysdir, int count);

Public string getsystemdirectory ()

{

Const int nchars = 128;

Stringbuilder buff = new stringbuilder;

GetsystemDirectory (BUFF, NCHARS);

Return buff.toString ();

}

Here we first reference the System.Runtime.InteropServices namespace and reference the API function GetSystemDirectory (StringBuilder, int). Finally, rewrote the method, the getSystemDirectory () method calls the API function and returns the system directory as a string.

2, parsing the XML file: This example uses the XML DOM (Document Object Modal) class to parse the XML file. The sequence is as follows:

USING SYSTEM.XML;

Private xmldocument Xmldoc = new xmldocument ();

PRIVATE STRINGFIGFILE;

Public systemSetting ()

{

Strconfigfile = getSystemDirectory () @ "/ system.config";

XMLDoc.Load (strconfigfile);

}

Public String getConfigValue (String Strnode, String Strattribute)

{

String strreturn = ""

Try

{

/ / Get the node according to the specified path

XMLNode XMLNode = xmldoc.selectsinglenode (STRNODE);

/ / Get the properties of the node and loop the required attribute value

XMlattributeCollection Xmlattr = XMLNode.Attribute;

For (INT i = 0; i

{

IF (xmlattr.Item (i) .Name == Strattribute)

Strreturn = Xmlattr.Item (i) .value;

}

}

Catch (XMLEXCEPTION XMLE)

{

Throw XMLE;

}

Return Strreturn;

}

Public String getConfigValue (String Strnode)

{

String strreturn = ""

Try

{

/ / Depending on the path acquisition node

XMLNode XMLNode = xmldoc.selectsinglenode (STRNODE);

Strreturn = xmlnode.innertext;}

Catch (XMLEXCEPTION XMLE)

{

System.console.writeline (XMLE.MESSAGE);

}

Return Strreturn;

}

Here we first reference the System.xml namespace, in the constructor, specify the configuration file to System.config in the system directory. Then use the XMLDocument's LOAD () method to read the file into the XMLDocument object XMLDoc.

GetConfigValue (String Strnode, String Strattribute) method reads the specified property value of the specified node. Such as the Server property of the node of the configuration file.

The getConfigValue method reads the value of the specified node, such as the value of the child node of the configuration file node.

Management configuration

The following program example provides three main methods for managing configuration files.

Public void setconfigValue (String Strnode, String NewValue)

{

Try

{

/ / Get the node according to the specified path

XMLNode XMLNode = xmldoc.selectsinglenode (STRNODE);

/ / Set the node value

XMLNode.innertext = newValue;

}

Catch (XMLEXCEPTION XMLE)

{

Throw XMLE;

}

}

Public void setconfigvalue (String Strnode, String Strattribute, String NewValue)

{

Try

{

/ / Get the node according to the specified path

XMLNode XMLNode = xmldoc.selectsinglenode (STRNODE);

/ / Get the properties of the node and loop the required attribute value

XMlattributeCollection Xmlattr = XMLNode.Attribute;

For (INT i = 0; i

{

IF (xmlattr.Item (i) .Name == Strattribute)

Xmlattr.Item (i) .value = newValue;

}

}

Catch (XMLEXCEPTION XMLE)

{

Throw XMLE;

}

}

Public void saveconfig ()

{

Try

{

/ / Save the result of the settings

XMLDoc.save (strconfigfile);

}

Catch (XMLEXCEPTION XMLE)

{

Throw XMLE;

}

}

SetConfigValue (String Strnode, String NewValue) is used to set the node value, setconfigValue (String StrNode, String Strattribute, String NewValue) to set the property value of the node. After modifying the configuration content, you must call the SaveConnfig () method to save the modified configuration to the file.

to sum up

There are many forms of formulation documents. This article is just a formulation file you have written. Of course, make a little modification for the program of this article, you can write other formulated files that are actually practical needs. I hope this article has some help from your development app.

Author: Pan Zhaoyong

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

New Post(0)