C # How to read the setting information in the ini file

xiaoxiao2021-03-06  82

// Write by www.wenhui.org

Change the following code to you, you can use it in your program, of course

Don't forget to add a name space.

Using system;

Using system.io;

Using system.Runtime.InteropServices;

Using system.text;

Namespace SX_MDI

{

///

/// summary description for class1.

///

Public class inIfile

{

// File INI name

PUBLIC STRING PATH;

Declare an API function to read and write ini files

[DLLIMPORT ("kernel32")]]

Private Static Extern long WritePrivateProfileString (String Section, String Key, String Val, String Filepath);

[DLLIMPORT ("kernel32")]]

Private static extern int GETPRIVATEPROFILESTRING (String Section, String Key, String DEF, STRINGBUILDER RETVAL, INT SIZE, STRING FILEPATH);

// Class constructor, pass the INI file name

Public inIfile (String InIPATH)

{

//

// Todo: add constructor logic here

//

Path = inipath;

}

// Write ini files

Public void IniwriteValue (String Section, String Key, String Value)

{

WritePrivateProfileString (Section, Key, Value, this.path);

}

// Read the INI file specified

Public String InireadValue (String Section, String Key)

{

StringBuilder Temp = New StringBuilder (255);

INT i = getPrivateProfileString (Section, Key, ", TEMP, 255, this.path;

Return Temp.toString ();

}

}

}

Operation example:

Public Static SqlConnection myconnection ()

{

String spath;

String ServerName, UserId, SPWD, Dataname

Spath = getpath ();

INIFILE INI = New InIfile (SPATH);

ServerName = Ini.inireAdValue ("Database", "Server");

Userid = INI.INIREADVALUE ("Database", "UID");

SPWD = Ini.inireAdValue ("Database", "PWD");

DataName = Ini.inireAdValue ("Database", "Database");

String strsql = "server =" servername "; uid =" userid "; pwd =; database =" DATANAME; SQLConnection myconn = new SqlConnection (strsql);

Return myconn;

}

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

New Post(0)