首页 Soft PlugIn RAN乱 Dev开发 Info资料 English WAP 留言 登陆 注册
-
Posted by Yippee | 评论(5) | 引用(0) | 阅读7072次
C#读写INI文件

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

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

  [Section]

  Key=Value

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

INIFILE类:

using System;www.shengfang.org

using System.IO;

using System.Rwww.shengfang.orguntime.InteropServices;

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

using System.Text;

namespace Ini

{

     publicclass IniFilewww.shengfang.org

     {

         publicstwww.shengfang.orgring path;     //INI文件名

          [DllImport("kernel32")]

          privatestaticexternlong WritePrivateProfileString(string section,string key,string val,string filePath);

          [DllImport("www.shengfang.orgkernel32")]

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

         //声明读写INI文件的API函数    

         public IniFile(string INIPath)

         { www.shengfang.org

              path = INIPath;

         }

         //类的构造函数,传递INI文件名

         publicvoid IniWriteValue(string Section,string Key,string Value)

         { www.shengfang.org

              WritePrivateProfileString(Section,Key,Value,this.path);

         }

         //写INI文件        

         publicstring IniReadValue(string Section,string Key)

         {

              StringBuilder twww.shengfang.orgemp = new StringBuilder(255);

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

              return temp.ToString();

         }

         //读取INI文件指定

     }

}
调用INIFILE类:

新建一个www.shengfang.org标准的C# WINDOWS应用程序项目,在窗体中分别增加命名为sect、key、val的三个文本框。

增加如下代码:

using Ini;    //创建命名空间

//当窗体关闭时保存www.shengfang.org窗体坐标

privatevoid Fwww.shengfang.orgorm1_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方法将数字转换为字符串

         }

//当窗体启动时,读取www.shengfang.orgINI文件的值并赋值给窗体

          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" )!=""))

              //判断返回值,避免第www.shengfang.org一次运行时为空出错

              {

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

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

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

                   this.Location =p;

              }

         }www.shengfang.org


字体:

Permanant URI永久地址 http://www.shengfang.org/blog/p/vsnetcsharpini.php
Trackback URI引用地址 http://www.shengfang.org/blog/tb.php?tb_id=1109292939

2005年2月25日08:55星期五  [Dev开发] 追踪此文的RSS
F@#k you.在 2008年12月15日23:30星期一 评论:
You are a motherf at #king idiot dot
EmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotionEmotion

Fuck在 2008年6月30日15:21星期一 评论:
真瓜皮,干脆别写才好,臭大粪一个,

路过在 2007年12月15日23:39星期六 评论:
AgreeEmotion

ww在 2007年3月23日13:16星期五 评论:
Emotion  wt the hell !!! why don't u just remove the godamn URL: www.shenfang.org.  that s so fking gross ,Disgusting.  u r so stupid. If u wanna publish sm technical aticles, put ur sick adds smwhere else.

smbody在 2007年1月24日04:47星期三 评论:

Yippee 回复于 2007年1月24日12:14
i n kw u wt sd

称呼:    登陆   注册
   不注册,但记住我的信息
邮件:
(非必须)
评论: [UBB代码帮助]
粗体 斜体 下划线 链接 水平线 引用



验证码: 请输入你看见的数字
关闭UBB      提交时自动将内容复制到剪贴板

公告





统计信息
[Yippee]||[统计]||日志:2143
在线: 17||用户: 2635 [列表]
今日:504||到访:3654289
Rss:||评论:1668

最新日志

最新评论

友情链接

日历
342009 - 0734
   1234
567891011
12131415161718
19202122232425
262728293031 

最新引用
搜索

归档

杂项
Get RSS Feed (Version 2.0)
Get Atom Feed (Version 0.3)
编码:  UTF-8