Use C # To Read and Write INI files!

Source: Internet
Author: User
You can call two APIs in kernel32.dll: writeprivateprofilestring and getprivateprofilestring to read INI files.

The specific implementation code is as follows:
Using System;
Using System. drawing;
Using System. collections;
Using System. componentmodel;
Using System. Windows. forms;
Using System. Data;
Using System. runtime. interopservices;
Using System. text;


Namespace Iniprocess
{

Public   Class Form1: system. Windows. Forms. Form
{
Private System. Windows. Forms. textbox textbox1;
Private System. Windows. Forms. Button button2;
Private System. Windows. Forms. Button button1;

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

Public   Void Iniwritevalue ( String Section, String Key, String Value, String Filepath) // The write operation on the INI file.
{
Writeprivateprofilestring (section, key, value, filepath );
}

Public   String Inireadvalue ( String Section, String Key, String Filepath) // A function used to read an INI file.
{
Stringbuilder temp= NewStringbuilder (255);
IntI=Getprivateprofilestring (section, key,"", Temp,
255, Filepath );
ReturnTemp. tostring ();

}




Private   Void Button#click ( Object Sender, system. eventargs E)
{

This. Textbox1. Text=Inireadvalue ("ODBC 32 bit data sources","MS Access Database","E: \ temp \ ODBC. ini");

}

Private   Void Button2_click ( Object Sender, system. eventargs E)
{

Iniwritevalue ("ODBC 32 bit data sources","MS Access Database",This. Textbox1. Text,"E: \ temp \ ODBC. ini");
}
}
}

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.