C#读写ini文件

本文介绍如何使用C#通过调用Win32API进行INI文件的读写操作,并提供了一个具体的示例代码,包括如何声明和调用Win32API函数。
主要思路是调用Win32 API。
1.引入命名空间
None.gif using  System.Runtime.InteropServices;
2.声明(把一个Win32 API函数转成C#函数)
        //声 明INI文件的写操作函数 WritePrivateProfileString()
        [DllImport( " kernel32 " )]
        
private   static   extern   long  WritePrivateProfileString( string  section,  string  key,  string  val,  string  filePath);

        
//声 明INI文件的读操作函数 GetPrivateProfileString()
        [DllImport( " kernel32 " )]
        
private   static   extern   int  GetPrivateProfileString( string  section,  string  key,  string  def, StringBuilder retVal,  int  size,  string  filePath);
3.调用
//写一个config.ini文件

private
  void  Save_Ini()
        {
            
string  s  =  System.Windows.Forms.Application.ExecutablePath;
            
// 在当前目录下,写一个config.ini文件
             string  path  =  s.ToLower().Replace( " mediaconvert.exe " " config.ini " );

            
string  configureNode  =   " DataBaseConfigure " ; // 配置节

            
string  key1  =   " DataBase " ; // 键名
             string  key1_Value  =   " DataBaseName " ; // 键值

            
string  key2  =   " Server " ;
            
string  key2_Value  =   " ServerName " ;

            
string  key3  =   " UserId " ;
            
string  key3_Value  =   " 1 " ;

            WritePrivateProfileString(configureNode, key1, key1_Value, path);
            WritePrivateProfileString(configureNode, key2, key2_Value, path);
            WritePrivateProfileString(configureNode, key3, key3_Value, path);
            

             /* 最后在exe文件的同目录下,生成一个config.ini文件,内容应如下:
             * [DataBaseConfigure]
             * DataBase=DataBaseName
             * Server=ServerName
             * UserId=1
             
*/
        }

//读取config.ini文件中的配置

private
  void  Read_Ini()
        {
            
string  s  =  System.Windows.Forms.Application.ExecutablePath;
            
// 取得config.ini路径
             string  path  =  s.ToLower().Replace( " mediaconvert.exe " " config.ini " );

            StringBuilder str 
=   new  StringBuilder( 255 );
            
// 取得配置节[DataBaseConfigure]的DataBase键的值
            GetPrivateProfileString( " DataBaseConfigure " " DataBase " "" , str,  255 , path);
            
// 对话框中结果应该为 DataBase:DataBaseName
            System.Windows.Forms.MessageBox.Show( " DataBase: "   +  str.ToString());
        }

C#使用系统Api,最头疼的问题就是Api中的数据类型在C#中,如何对应的问题。
这个网站http://www.pinvoke.net列出了大多系统Api在此C#或VB.Net中的对应声明,很详细。

C#读写 INI 文件可以通过自定义方法或使用第三方库实现。INI 文件是一种常见的配置文件格式,结构简单、易于理解。 以下是通过编写代码手动操作 INI 文件的方式: ### 1. 使用 Windows API 操作 INI 文件 Windows 提供了 `GetPrivateProfileString` 和 `WritePrivateProfileString` 函数来处理 INI 配置文件。可以在 C# 程序中调用这些函数完成对 INI读写。 #### 示例代码: ```csharp using System; using System.Runtime.InteropServices; public class IniFileHandler { [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defaultValue, StringBuilder retval, int size, string filePath); [DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string value, string filePath); public string FilePath { get; set; } // 构造函数初始化路径 public IniFileHandler(string iniFilePath) { this.FilePath = iniFilePath; } // 写入值到INI文件 public void WriteValue(string section, string key, string value) { WritePrivateProfileString(section, key, value, this.FilePath); } // 从INI文件读取指定键的值 public string ReadValue(string section, string key) { StringBuilder sb = new StringBuilder(256); // 定义缓冲区大小 int length = GetPrivateProfileString(section, key, "", sb, sb.Capacity, this.FilePath); return sb.ToString(); } } // 测试示例 class Program { static void Main() { var handler = new IniFileHandler(@"test.ini"); // 写入数据 handler.WriteValue("Section1", "Key1", "Value1"); // 读取数据 string readResult = handler.ReadValue("Section1", "Key1"); Console.WriteLine($"Read Value: {readResult}"); } } ``` --- ### 2. 自己解析字符串内容 (无依赖) 如果不想引入外部依赖项或者系统API,则可以将 `.ini` 视为普通的文本文件,并自行实现逻辑来加载和保存其中的内容。 #### 范例 - 手动读写 INI 文件 ```csharp System.Collections.Specialized.StringCollection settingsInIniFormat = new System.Collections.Specialized.StringCollection(); settingsInIniFormat.Add("[SectionA]"); settingsInIniFormat.Add("key=value"); ... ``` 然后逐行分析存储的数据并将其转换回字典形式等更易访问的形式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值