Opening the INI file under .NET

zhaozj2021-02-16  56

Using system; using system.text; using system.Runtime.Interopservices;

Namespace fractalist.common {////

/// Operate the class / ////////

Public class operateini

{

Public OPERATEINI ()}

Private const string defaultValue = "Unable to read the specified value";

Private static stringbuilder retval = new stringbuilder (255);

///

/// Win32 reads the API function of the INI file ////

[DLLIMPORT ("kernel32")]]

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

///

/// Read the method of the INI configuration file ///

///

Paragraph name

///

Keyword

///

Default value when you can't read

///

Read value

///

Value size

///

The full path and name of the INI file

///

Private static int GETPRIVATEPROFILESTRING (String Section, String Key, String Def, StringBuilder Retval, Int size, String Filepath)

{

Return GetPrivateProfileString (Section, Key, DEF, RETVAL, SIZE, FILEPATH);

}

///

/// Win32 Modify the API function of the INI file ///

[DLLIMPORT ("kernel32")]]

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

///

/// Write the method of the INI configuration file (if you read only the read-only file properties, write to the file, then modify the file attribute is read-only) ///

///

Paragraph name

///

Keyword

///

To be written

///

The full path and name of the INI file

///

Private Static Long SetPrivateProfileString (String Section, String Key, String Val, String Filepath)

{

Long longval = 0;

IF (Filettributes.Readonly & File.GetaTRibutes (FilePath)) == FileAttributes.Readonly

{

File.setttributes (FilePath, FileAttributes.Normal);

Longval = WritePrivateProfileString (Section, Key, Val, FilePath);

File.setttributes (FilePath, FileAttributes.Readonly);

}

Else

{

Longval = WritePrivateProfileString (Section, Key, Val, FilePath);

}

Return longval;

}

Private static string getValueonly (string val) {string [] testArray = val.split (new char [] {'#',}); return testArray [0];}}}

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

New Post(0)