Class for reading and writing INI files, calling the API in kernel32.dll

Source: Internet
Author: User

At this time, I found that cainiao is now learning how to write old birds and referencing old birds.Article

 

ReferenceHttp://www.pin5i.com/showtopic-8418.html

 

Code

Using System. runtime. interopservices;
Using System. text;

Namespace INIFILE
{
/// <Summary>
/// Class for reading and writing INI files
/// Call two APIs in kernel32.dll: writeprivateprofilestring and getprivateprofilestring to read and write INI files.
/// The INI file is a text file,
/// Consists of several sections,
/// Under each title with parentheses,
/// Is a number of keywords and their corresponding values)
/// [Section] Key = Value
///   </Summary>

Public Class INIFILE
{
/// <Summary>
/// INI File Name (with Path)
/// </Summary>

Public String Filepath; // Declare the API function for reading and writing 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 );

/// <Summary>
/// Class Constructor
/// </Summary>
/// <Param name = "inipath"> INI File Name </Param>
Public INIFILE ( String Inipath)
{
Filepath = Inipath;
}

/// <Summary>
/// Write an INI File
/// </Summary>
/// <Param name = "section"> Section </Param>
/// <Param name = "key"> Key </Param>
/// <Param name = "value"> Value </Param>
Public Void Writeinivalue ( String Section, String Key, String Value)
{
Writeprivateprofilestring (section, key, value, This . Filepath );
}

/// <Summary>
/// Read specified part of the INI File
/// </Summary>
/// <Param name = "section"> Section </Param>
/// <Param name = "key"> Key </Param>
/// <Returns> String </Returns>
Public String Readinivalue ( String Section, String Key)
{
Stringbuilder temp = New Stringbuilder ( 255 );
Int I = Getprivateprofilestring (section, key, "" , Temp, 255 , This . Filepath ); Return Temp. tostring ();
}
}
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.