惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Visual Studio Blog
Google DeepMind News
Google DeepMind News
V
V2EX
B
Blog RSS Feed
有赞技术团队
有赞技术团队
博客园 - Franky
美团技术团队
月光博客
月光博客
酷 壳 – CoolShell
酷 壳 – CoolShell
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
腾讯CDC
云风的 BLOG
云风的 BLOG
L
LangChain Blog
GbyAI
GbyAI
The Cloudflare Blog
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
C
Check Point Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Stack Overflow Blog
Stack Overflow Blog
博客园 - 【当耐特】
The Register - Security
The Register - Security
大猫的无限游戏
大猫的无限游戏
D
Docker
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
P
Proofpoint News Feed
N
Netflix TechBlog - Medium
博客园_首页
A
About on SuperTechFans
J
Java Code Geeks
量子位
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
MongoDB | Blog
MongoDB | Blog
Recent Announcements
Recent Announcements
G
Google Developers Blog
小众软件
小众软件
博客园 - 叶小钗
WordPress大学
WordPress大学
博客园 - 聂微东
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Martin Fowler
Martin Fowler
S
SegmentFault 最新的问题
F
Full Disclosure
Jina AI
Jina AI
H
Help Net Security

博客园 - 老弹

列表中,将列表字段缩略显示的样式 DIV+CSS页面布局,样式中区别苹果浏览器Safari VS.NET 2008 发布站点,不带那些随机字符的网站发布插件 下拉菜单被flash挡住的最好解决方法 html导出word - 老弹 - 博客园 全球网络概况 IE不执行js脚本的解决方法 ToString格式大全 - 老弹 - 博客园 SQL语句关键字UNION小知识 关于PayPal的支付的问题 运行 Dcpromo 架设Active Directory (活动目录 )时,出现" 无法创建 GPO " 错误信息 document.location 这个对象包含了当前URL的信息 VBScript判断客户端是否需要下载控件 强制将在表格中将英文字母换行的CSS样式,加在表格table标签中 无法让网页另存为的代码 屏蔽IE中当图片太大时出现的图片按钮组的CSS样式 列出一个表中的某个字段重复的记录 分割以逗号作为分割符号的字符串并插入到表中 求助 RemObjects SDK 与 Data Abstracr
.NET读写INI文件
老弹 · 2006-05-01 · via 博客园 - 老弹
 

.NET读写INI文件

microblue 2005-4-5 03:52  

虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。 

INI文件是文本文件,由若干节(section)组成,在每个带括号的标题下面,是若干个关键词(key)及其对应的值(Value)

   [Section]

   Key=Value 

VC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面我介绍一个读写INI文件的C#类并利用该类保存窗体的坐标,当程序再次运行的时候,窗体将显示在上次退出时的位置。

INIFILE类: 

using System;
using System.IO;
using System.Runtime.InteropServices; 

因为我们需要调用API函数,所以必须创建System.Runtime.InteropServices命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。 

using System.Text; 
namespace Ini
{ 

   publicclass IniFile
   { 

     publicstring path;   //INI文件名
     [DllImport("kernel32")]
     privatestaticexternlong WritePrivateProfileString(string section,string key,string val,string filePath);
     [DllImport("kernel32")] 

     privatestaticexternint GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath); 

     //声明读写INI文件的API函数   
        public IniFile(string INIPath)
       {
       path = INIPath;
     }

     //类的构造函数,传递INI文件名
        publicvoid IniWriteValue(string Section,string Key,string Value)
     {
        WritePrivateProfileString(Section,Key,Value,this.path);
     } 

     //INI文件 

     publicstring IniReadValue(string Section,string Key)
       {
          StringBuilder temp = new StringBuilder(255);

       int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); 

       return temp.ToString(); 

     }

     //读取INI文件指定 

   } 

} 

调用INIFILE类: 

新建一个标准的C# WINDOWS应用程序项目,在窗体中分别增加命名为sectkeyval的三个文本框。 

增加如下代码:

using Ini;  //创建命名空间

//当窗体关闭时保存窗体坐标 

                  privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e)
     {

       IniFile ini = new IniFile("C:\\test.ini"); 

       ini.IniWriteValue("LOC" ,"x" ,this.Location.X.ToString()  ); 

       ini.IniWriteValue("LOC " ,"y" ,this.Location.Y.ToString()  ); 

       //ToString方法将数字转换为字符串 

     } 

//当窗体启动时,读取INI文件的值并赋值给窗体 

     privatevoid Form1_Load(object sender, System.EventArgs e)
      {

       IniFile ini = new IniFile("C:\\test.ini"); 

       Point   p=new Point() ; 

       if ((ini.IniReadValue ("LOC" ,"x" )!="" ) && (ini.IniReadValue ("LOC" ,"y" )!="")) 

       //判断返回值,避免第一次运行时为空出错
       {

          p.X=int.Parse (ini.IniReadValue ("LOC" ,"x" ));

          p.Y =int.Parse (ini.IniReadValue ("LOC" ,"y" )); 

          // int.Parse将字符串转换为int

          this.Location =p; 

       }

     }  

可以通过调用kernel32.dll中的两个apiWritePrivateProfileStringGetPrivateProfileString来实现对ini 文件的读些。

具体实现的代码如下:
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)//ini文件进行写操作的函数
      {
           WritePrivateProfileString(Section,Key,Value,filepath);
      }
        
      public string IniReadValue(string Section,string Key,string filepath)//
ini文件进行读操作的函数
      {
           StringBuilder temp = new StringBuilder(255);
           int i = GetPrivateProfileString(Section,Key,"",temp, 255, filepath);
           return temp.ToString();

      }

 }