ini文件

C# 读写.ini配置文件

倾然丶 夕夏残阳落幕 提交于 2019-11-29 22:07:37
`class Ini { // 声明INI文件的写操作函数 WritePrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern long WritePrivateProfileString(string section, string key, string val, string filePath); // 声明INI文件的读操作函数 GetPrivateProfileString() [System.Runtime.InteropServices.DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string def, System.Text.StringBuilder retVal, int size, string filePath); public static bool Write(string section, string key, string value, string sPath) { // section=配置节,key=键名,value=键值,path=路径 long

c#实现INI文件操作

泄露秘密 提交于 2019-11-29 22:07:24
INI 文件其实是一种具有特定结构的文本文件,它的构成分为三部分,结构如下 [Section1] key 1 = value2 key 1 = value2 …… [Section2] key 1 = value1 key 2 = value2 文件由若干个段落(section)组成,每个段落又分成若干个键(key)和值(value)。Windows系统自带的Win32的API函数GetPrivateProfileString()和WritePrivateProfileString()分别实现了对INI文件的读写操作,他们位于kernel32.dll下。 但是令人遗憾的是C#并没有提供相关INI文件操作的函数,所以唯一比较理想的方法就是调用API函数。 接下来我就简单讲解下 函数GetPrivateProfileString()和WritePrivateProfileString()的参数讲解 读取操作: [DllImport( " kernel32 " )] private static extern int GetPrivateProfileString( string section, string key, string defVal, StringBuilder retVal, int size, string filePath); section:要读取的段落名 key

C# INI文件的读取

对着背影说爱祢 提交于 2019-11-29 22:07:15
下面举一个常见例子,数据库连接配置INI文件,首先创建config.ini文件,输入如下: [Database] Server=127.0.0.1 DB=test_bak User=sa Password=123456 创建一个类,如ReadIni,C#使用Ini文件时需要使用API函数。 # region API函数声明 [DllImport( "kernel32" )] //返回0表示失败,非0为成功 private static extern long WritePrivateProfileString ( string section, string key, string val, string filePath); [DllImport( "kernel32" )] //返回取得字符串缓冲区的长度 private static extern long GetPrivateProfileString ( string section, string key, string def, StringBuilder retVal, int size, string filePath); # endregion 读取INI方法 public string ReadIniData ( string Section, string Key, string NoText, string

C#读写ini文件详解

落爺英雄遲暮 提交于 2019-11-29 22:06:46
C#读写ini文件详解 C#读写ini文件是如何进行的呢?C#读写ini文件需要的方法有哪些呢?本文就向你一一介绍。 C#读写ini文件之前要了解的概念:INI就是扩展名为"INI"的文件,其实他本身是个文本文件,可以用记事本打开,主要存放的是用户所做的选择或系统的各种参数. C#读写ini文件其实并不是普通的文本文件.它有自己的结构.由若干段落(SECTION)组成,在每个带括号的标题下面,是若干个以单个单词开头的关键字(KEYWORD)和一个等号,等号右边就是关键字的值(VALUE).例如: [Section1] KeyWord1 = Value1 KeyWord2 = Value2 ... [Section2] KeyWord3 = Value3 KeyWord4 = Value4 C#读写ini文件最初的想法:C#命名空间中没有直接读写INI的类,当然如果你把INT当成文本文件用System.IO类来读写算我没说. 我现在介绍的是系统处理INI的方法. 虽然C#中没有,但是在"kernel32.dll"这个文件中有Win32的API函数--WritePrivateProfileString()和GetPrivateProfileString() C#读写ini文件实现之C#声明INI文件的写操作函数WritePrivateProfileString():

C# ini文件读写类

萝らか妹 提交于 2019-11-29 22:06:21
VC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面是一个C# ini文件读写类, 从网上收集的,很全,就是没有对section的改名功能,高手可以增加一个。 using System; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Collections; using System.Collections.Specialized; namespace wuyisky{   /**//**/   /**//// <summary>   /// IniFiles的类   /// </summary>   public class IniFiles   {     public string FileName; //INI文件名     //声明读写INI文件的API函数     [DllImport("kernel32")]     private static extern bool WritePrivateProfileString(string section, string key, string val, string filePath);     [DllImport("kernel32")]    

C# 读写Ini文件,全代码示例解析

给你一囗甜甜゛ 提交于 2019-11-29 22:05:35
ini文件在Win95以前比较盛行,之后由于出册表等技术的出现,ini技术主键退居二线,不过对于一些小项目,读写ini文件还是很适用的。 Windows API提供了读写配置文件的操作,在C#程序中只要导入相应的API即可。例如GetPrivateProfileString()方法,在MSDN查得原型如下: DWORD GetPrivateProfileString( LPCTSTR lpAppName, // section name LPCTSTR lpKeyName, // key name LPCTSTR lpDefault, // default string LPTSTR lpReturnedString, // destination buffer DWORD nSize, // size of destination buffer LPCTSTR lpFileName // initialization file name ); 由于C#和C++数据类型不同,在导入win32 API时需要做相应的转换,并且要导入相关的类。如下所示: [DllImport("kernel32")] private static extern int GetPrivateProfileString(string section, string key, string defVal,

C# ini文件读写 类

筅森魡賤 提交于 2019-11-29 22:04:50
一个C# ini文件读写类,从网上收集的,很全,就是没有对section的改名功能,高手可以增加一个 using System; using System.IO; using System.Runtime.InteropServices; using System.Text; using System.Collections; using System.Collections.Specialized; namespace wuyisky { /**/ /// <summary> /// IniFiles的类 /// </summary> public class IniFiles { public string FileName; // INI文件名 // 声明读写INI文件的API函数 [DllImport("kernel32")] private static extern bool WritePrivateProfileString( string section, string key, string val, string filePath); [DllImport("kernel32")] private static extern int GetPrivateProfileString( string section, string key, string def,

Winform配置文件分类及使用

我的未来我决定 提交于 2019-11-28 14:52:59
以前只用过 xml 配置文件,最近项目需要使用配置文件,才开始学习配置文件的分类及使用,具体如下。 1.Properties-Settings.settings 在 winform 项目下的 Properties 中有 Settings.settings 文件,双击即可打开文件,如图: 1.1 解释说明 名称(Name):相当于变量名称一样。 类型( Type ): 配置的 Settings 的类型 。 范围( Scope ): 用户和应用程序,用户则运行时可更改,应用程序则运行时不可更改。     范围为 “ 应用程序 ” 的属性,读取都是从 App.config 里获取,设置也可以通过手工修改 App.config 改变,但是在程序中无法对其进行赋值,只能读取。     而范围为 “ 用户 ” 的属性 Settings 在第一次运行时会读取 App.config 里的初始值 。 但是一旦调用 Save 方法后, Settings 里 “ 用户 ” 范围的属性就会保存在系统里面,类似 WebForm 里的 Cookies 一样 。 从此以后,读取都会从系统里保存的值里读取,手工修改 App.config 里的 “ 用户 ” 范围的属性不会影响到这些属性,但是在调用 Reset 方法时会 重新 从 App.config 里获取 “ 用户 ” 范围的属性写入到系统中。也就是说,

C# INI配置文件读写

纵然是瞬间 提交于 2019-11-28 07:23:50
using System; using System.Collections.Generic; using System.Linq; using System.Runtime.InteropServices; using System.Text; using System.IO; class IniConfig { private string inipath = AppDomain.CurrentDomain.BaseDirectory + "Config.ini"; public bool CanRead() { if (File.Exists(inipath)) { return true; } return false; } //声明API函数 [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

python ini文件处理

百般思念 提交于 2019-11-27 21:44:05
python 操作配置文件ini的三种方法 方法一:crudini 命令 crudini命令是Linux下的一个操作配置文件的命令工具 方法二 :ConfigParser模块 ConfigParser 模块为常用的操作ini文件的模块,但是存在一些缺陷,无法识别section的大小写,无法读取文件注释,这样修带有注释的配置文件时就会存在问题。 方法三:configobj模块 正常的读配置文件的方法是给ConfigObj一个文件名,然后通过字典来访问成员,子段来获取value值,不会存在注释无法读取的缺陷 来源: https://www.cnblogs.com/longchang/p/11378828.html