C # read and write ini files

xiaoxiao2021-03-06  20

Author: bloom http://oraasp.vicp.net/article/article.aspx?ID=26

Although Microsoft has already suggested instead of Ini files in Windows, in practical applications, Ini files still use martial arts, especially now the popularity of green software, more and more programs save some of their configuration information to INI Document.

The INI file is a text file, which is composed of several sections. Here's a keyword (key) and its corresponding value (Value):

[Section]

Key = Value

The API function is provided in the VC to read and write the INI file, but the C # programming language launched by Microsoft has no corresponding method. Here I introduce a C # class read and write the INI file and use the class to save the form of the form. When the program runs again, the form will appear when the last exit.

InIfile class:

Using system;

Using system.io;

Using system.Runtime.InteropServices;

Because we need to call the API function, you must create a collection of System.Runtime.InterOpServices to provide a collection of classes that can be used to access the COM objects in .NET and the class of the local API.

Using system.text;

Namespace Ini

{

Public class inIfile

{

Public String path; // ini file name

[DLLIMPORT ("kernel32")]]

Private Static Extern long writPrivateProfileString (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;

// Declare the API function to read the INI file

Public inIfile (String InIPATH)

{

Path = inipath;

}

// Class constructor, pass the INI file name

Publicvoid IniWriteValue (String Section, String Key, String Value)

{

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

}

// Write ini files

PUBLICSTRING InireadValue (String Section, String Key)

{

StringBuilder Temp = New StringBuilder (255);

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

Return Temp.toString ();

}

// Read the INI file specified

}

}

Call inIfile class:

Newly built a standard C # Windows application project, add three text boxes named SECT, KEY, and VAL in the form.

Added code:

Using ini; // Create a namespace

// Save the form coordinates when the form is closed

Privatevoid form1_closing (Object Sender, System.comPonentmodel.canceleventargs E)

{

INIFILE INI = New InIfile ("c: //test.ini"); Ini.iniWriteValue ("LOC", "X", this.location.x.tostring ());

INI.INIWRITEVALUE ("Loc", "Y", this.location.y.tostring ());

// Tostring method converts the digital to a string

}

/ / When the form is started, the value of the INI file is read and assigned to the form.

Privatevoid Form1_Load (Object Sender, System.EventArgs E)

{

INIFILE INI = New InIfile ("C: //Test.ini");

Point P = new point ();

/ / Judgment return value, avoid errors when running for the first time

IF ((INI.INIREADVALUE ("LOC", "X")! = "" && (Ini.inireAdValue ("LOC", "Y")! = "")

{

P.x = int.parse (INI.INIREADVALUE ("LOC", "X"));

p.y = int.parse (INI.INIREADVALUE ("LOC", "Y"));

// int.parse converts the string to INT

THIS.LOCATION = P;

}

}

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

New Post(0)