Read and Write ini configuration files in C #

Source: Internet
Author: User

Management configuration is essential for application system development. For example, configure, install, and update the database server. Due to the rise of XML, most configuration files are stored as XML files. For example, Visual Studio. NET's own configuration file mashine. config, ASP. NET's configuration file web. config, including the configuration files I mentioned in remoting, are all in XML format.
 

The traditional configuration file INI has been gradually replaced by XML files, but for simple configuration, the INI file is still useful. The INI file is actually a text file, which has a fixed format. The section name is enclosed in [], and the key value is displayed in line feed:
[Section]
Key = Value
 

For example, the database server configuration file:
 

Dbserver. ini
 

[Server]
Name = localhost
[DB]
Name = northwind
[User]
Name = sa
 

In C #, the configuration file is read and written through API functions, and the code is very simple:

Code:
  1. Using system;
  2. Using system. text;
  3. Using system. IO;
  4. Using system. runtime. interopservices;
  5. Namespace pubop
  6. {
  7. Public class operateinifile
  8. {
  9. API function declaration # region API function declaration
  10. [Dllimport ("Kernel32")] // if the return value is 0, the operation fails. If the return value is not 0, the operation succeeds.
  11. Private Static extern long writeprivateprofilestring (string section, string key,
  12. String Val, string filepath );
  13. [Dllimport ("Kernel32")] // returns the length of the string buffer obtained.
  14. Private Static extern long getprivateprofilestring (string section, string key,
  15. String def, stringbuilder retval, int size, string filepath );
  16.  
  17.  
  18. # Endregion
  19. Read the INI file # region read the INI File
  20. Public static string readinidata (string section, string key, string notext, string inifilepath)
  21. {
  22. If (file. exists (inifilepath ))
  23. {
  24. Stringbuilder temp = new stringbuilder (1024 );
  25. Getprivateprofilestring (section, key, notext, temp, 1024, inifilepath );
  26. Return temp. tostring ();
  27. }
  28. Else
  29. {
  30. Return string. empty;
  31. }
  32. }
  33.  
  34. # Endregion
  35. Write the INI file # region write the INI File
  36. Public static bool writeinidata (string section, string key, string value, string inifilepath)
  37. {
  38. If (file. exists (inifilepath ))
  39. {
  40. Long opstation = writeprivateprofilestring (section, key, value, inifilepath );
  41. If (opstation = 0)
  42. {
  43. Return false;
  44. }
  45. Else
  46. {
  47. Return true;
  48. }
  49. }
  50. Else
  51. {
  52. Return false;
  53. }
  54. }
  55.  
  56. # Endregion
  57. }
  58. }

Briefly describe the parameters of writeinidata () and readinidata () in the following methods.
 

The Section parameter, key parameter, and inifilepath are unnecessary. The value parameter indicates the key value. The notext parameter corresponds to the def parameter of the API function, and the value is specified by the user, when no specific value is found in the configuration file, it is replaced by the value of notext.

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.