C#读写ini文件

主要思路是调用Win32 API。

1.引入命名空间

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#中,如何对应的问题。

这个网站(www.pinvoke.net)列出了大多系统Api在此C#或VB.Net中的对应声明,很详细。

时间: 2024-08-30 13:10:00

C#读写ini文件的相关文章

如何在C#中读写INI文件

INI文件就是扩展名为"ini"的文件.在Windows系统中,INI文件是很多,最重要的就是"System.ini"."System32.ini"和"Win.ini".该文件主要存放用户所做的选择以及系统的各种参数.用户可以通过修改INI文件,来改变应用程序和系统的很多配置.但自从Windows 95的退出,在Windows系统中引入了注册表的概念,INI文件在Windows系统的地位就开始不断下滑,这是因为注册表的独特优点

C#中读写ini文件

C#中没有读写ini文件的类,但用API函数很容易实现. 窗体代码如下: using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Runtime.InteropServices; namespace IniFile

[c#]:如何在C#中读写INI文件(二)

C#中读写INI文件的关键步骤和解决方法 C#对INI文件进行写操作: 对INI文件进行写操作,是通过组件button2的"Click"事件来实现的.这里有一点应该注意,当在调用WritePrivateProfileString()对INI文件进行写操作的时候,如果此时在INI文件中存在和要写入的信息相同的段落名称和关键字,则将覆盖此INI信息.下面是button2组件的"Click"事件对应的代码清单: private void button2_Click ( o

在.net中读写INI文件

net中读写ini文件和Vb6中的做法是一致的,唯一注意的一点是Api声明中的Long型变量要改为int32类型在.net中 Private Declare Function GetPrivateProfileString Lib "kernel32" Alias "GetPrivateProfileStringA" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefa

常用的读写ini文件的类

using System;using System.IO;using System.Runtime.InteropServices;using System.Text;using Microsoft.Win32; namespace Wjb.ReadOrWriteIniAndReg{ /// <summary> /// RWIni 的摘要说明. /// 读写ini文件类 /// 类库开发:吴剑冰 /// 时间:2003年10月20日 /// 功能:读写INI文件 /// </summar

C#中读写INI文件的方法

通常C#使用基于XML的配置文件,不过如果有需要的话,比如要兼顾较老的系统,可能还是要用到INI文件. 但C#本身并不具备读写INI文件的API,只有通过调用非托管代码的方式,即系统自身的API才能达到所需的目的. 对应读写的方法分别为GetPrivateProfileString和WritePrivateProfileString. GetPrivateProfileString中的各参数: lpAppName -- section的名称 lpKeyName -- key的名称 lpDefau

Python读写ini文件的方法

  本文实例讲述了Python读写ini文件的方法.分享给大家供大家参考.具体如下: 比如有一个文件update.ini,里面有这些内容: ? 1 2 3 4 5 6 7 8 [ZIP] EngineVersion=0 DATVersion=5127 FileName=dat-5127.zip FilePath=/pub/antivirus/datfiles/4.x/ FileSize=13481555 Checksum=6037,021E MD5=aaeb519d3f276b810d46642

刚学c++的小白,在网上看到一段读写ini文件的代码,想试试结果有错。

问题描述 刚学c++的小白,在网上看到一段读写ini文件的代码,想试试结果有错. #include "stdafx.h" #include #include #include using namespace std; int main() { LPTSTR lpPath= new char[MAX_PATH]; strcpy(lpPath, "D:\IniFileName.ini"); WritePrivateProfileString("LiMing&q

读写ini文件,64位windows操作系统

问题描述 读写ini文件,64位windows操作系统 我在windows 64位操作系统下,读写ini文件,存放于系统盘的%temp%目录下,但是老师崩溃的.WritePrivateProfileString(""Section1""SecondKey"",""By golly it works!"",%temp%目录); 我查看msdn,说是要RegCreateKeyEx创建注册表,然后要RegSetV

在 WinCe 平台读写 ini 文件

      在上篇文章开发 windows mobile 上的今日插件时,我发现 wince 平台上不支持例如 GetPrivateProfileString 等相关 API 函数.在网络上我并没有找到令我满意的相应代码,因此我手工自己写了相应的方法.命名规则是,在 PC API 函数的名称前面加上 "Ce" 前缀,这是为了在 PC 平台上调试和使用时,不和系统的 API 函数发生冲突.值得注意的是,在写 CeWritePrivateProfileString 方法时,如果改写后的 i