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

推荐订阅源

T
The Blog of Author Tim Ferriss
Know Your Adversary
Know Your Adversary
P
Palo Alto Networks Blog
D
Darknet – Hacking Tools, Hacker News & Cyber Security
K
Kaspersky official blog
L
LINUX DO - 热门话题
P
Proofpoint News Feed
P
Privacy & Cybersecurity Law Blog
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
Cisco Talos Blog
Cisco Talos Blog
AI
AI
L
LINUX DO - 最新话题
H
Heimdal Security Blog
Hacker News: Ask HN
Hacker News: Ask HN
Webroot Blog
Webroot Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
The GitHub Blog
The GitHub Blog
I
Intezer
Blog — PlanetScale
Blog — PlanetScale
有赞技术团队
有赞技术团队
S
Securelist
博客园_首页
IT之家
IT之家
Schneier on Security
Schneier on Security
博客园 - 叶小钗
罗磊的独立博客
WordPress大学
WordPress大学
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
MongoDB | Blog
MongoDB | Blog
P
Proofpoint News Feed
阮一峰的网络日志
阮一峰的网络日志
A
Arctic Wolf
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
W
WeLiveSecurity
The Register - Security
The Register - Security
D
DataBreaches.Net
S
Security @ Cisco Blogs
Security Archives - TechRepublic
Security Archives - TechRepublic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
腾讯CDC
Recorded Future
Recorded Future
NISL@THU
NISL@THU
N
News and Events Feed by Topic
T
Tailwind CSS Blog
N
News and Events Feed by Topic
Cyberwarzone
Cyberwarzone
T
Tor Project blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com

博客园 - 元本如此

从定制软件到通用软件的转变 小记 流程运转核心算法 流程运转 培训有感 Flash MX 2004数据库应用程序开发——基于.NET架构 Be Your Personal Best 关于工作流 再次归来 杨少华,我是喜欢的 仁者见仁,智者见智 看世界 应用程序设计指南:从 N 层到 .NET 剖析 ADO.NET 批处理更新(深入研究数据访问) 关于何种情况下使用DataGrid、DataList或Repeater的一些讨论 把信送给加西亚 软件之死 软件的涅磐 第二次丢钱包
C#读写INI文件
元本如此 · 2004-07-07 · via 博客园 - 元本如此
 

虽然微软早已经建议在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

{

     
public class IniFile

     
{

         
public string path;    //INI文件名

         [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);

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

     

         
public IniFile(string INIPath)

         
{

              path 
= INIPath;

         }


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

         
public void IniWriteValue(string Section,string Key,string Value)

         
{

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

         }


         
//写INI文件

         

         
public string 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;    //创建命名空间

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

private void 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文件的值并赋值给窗体

         
private void 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;

              }


         }