Use a C # operation ini file

zhaozj2021-02-16  49

Use a C # operation ini file

Original: Blazinix Translation: DragonTt

This class, encapsulates the methods provided in kernal32.dll to operate the INI file.

Introduction:

A class is created here, encapsulating two methods provided in kernel32.dll, used to operate the INI file. These two methods are: WritePrivateProfileString and GetPrivateProfileString.

The namespaces that need references are: system.Runtime.InterOpServices and System.Text

Class source file

Using system;

Using system.Runtime.InteropServices;

Using system.text;

Namespace Ini

{

///

/// Create a new ini file to store or loading data

///

Public class inIfile

{

PUBLIC STRING PATH;

[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;

///

/// inIfile constructor.

///

///

Public inIfile (String InIPATH)

{

Path = inipath;

}

///

/// write data to the in iNi file

///

///

/// section name

///

/// Key Name

///

/// Value Name

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

{

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

}

///

/// read data value from the ini file

///

///

///

///

/// public string inireadvalue (String section, string key)

{

StringBuilder Temp = New StringBuilder (255);

INT i = getPrivateProfileString (Section, Key, ", TEMP,

255, this.path);

Return Temp.toString ();

}

}

}

Use this class

Follow these steps:

1. Add a reference to namespaces in your project

Using ini;

2. Create a following INIFILE object

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

3. Use the INIWRITEVALUE method to give a key payment value in the specified configuration, or use the InireAdValue method to read the value of a key in the specified configuration section.

As mentioned above, it is very easy to encapsulate the API function into your class in C #.

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

New Post(0)