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

推荐订阅源

N
Netflix TechBlog - Medium
G
Google Developers Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
T
The Blog of Author Tim Ferriss
Microsoft Azure Blog
Microsoft Azure Blog
GbyAI
GbyAI
L
LangChain Blog
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
aimingoo的专栏
aimingoo的专栏
P
Proofpoint News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
小众软件
小众软件
WordPress大学
WordPress大学
A
About on SuperTechFans
大猫的无限游戏
大猫的无限游戏
C
Check Point Blog
月光博客
月光博客
Stack Overflow Blog
Stack Overflow Blog
美团技术团队
Jina AI
Jina AI
T
Tailwind CSS Blog
Google DeepMind News
Google DeepMind News
D
Docker

博客园 - 幸福★星

开心网外挂,开心网自动管理程序,现已经开发完成 Flex 图片 幻灯片效果初探 FDS.SWC 下载 解决使用FluorineFx实现Remoting通讯时 报 "Channel definition, mx.messaging.channels.RTMPChannel, can not be found" 错的问题 [转载]Flex 2.0 实现SWF全屏 - 幸福★星 [原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (四) [原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (三) [原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (二) [原创]Flex 与 Asp.Net 通过 Remoting 方式进行通讯 (一) [原创]简单的读写注册表配置类 [原创]简单的XML配置文件读写类 VB 6.0 利用CopyMemory实现 指针功能 Sql Server 2000 中 将 0,1 字段,换成 是,否 的查询语句 [原创]C# 操作Excel的类 MSN病毒"性感相册"一个变种的手工杀毒方法 TFS 查看工作区,删除工作区,删除项目 开如使用TFS VS.NET 2005 SP1 安装注意 C# 系统热键类的修正
[原创]C#不调用API读写INI文件类
幸福★星 · 2007-09-13 · via 博客园 - 幸福★星

刚写完读XML配置的类,突发奇想,想写一个不调用API来读INI文件的类,经过一点点努力,终于实现了,现将代码帖出。

http://xingfustar.cnblogs.com

/*----------------------------------------------------------------
 * 版权:
http://XingFuStar.cnblogs.com
 * 
 * 文件名: IniConfig
 * 文件功能描述: 不调用API读写INI文件类
 * 
 * 作者:XingFuStar
 * 日期:2007年9月13日
 * 
 * 当前版本:V1.0.0
 * 
 * 修改日期:
 * 修改内容:
 *---------------------------------------------------------------
*//*----------------------------------------------------------------
 * 
 * 为保证代码的正确性,对INI文件做如下要求:
 * 1、以";"开头代表注释,注释要另起一行,不要使用非";"开头的注释
 * 2、如果使用了非";"开头的注释,该行中不要包括“=”
 * 3、如果使用了非";"开头的注释,对INI进行设置生,该行会被删除
 * 
 *---------------------------------------------------------------
*/using System;
using System.Collections;
using System.Diagnostics;
using System.IO;namespace XingFuStudio.Config
{
    
class IniConfig
    {
        
private string iniPath = "";
        
private bool isConfig;
        
private ArrayList propertyList;/// <summary>
        
/// 构造函数:装载配置文件
        
/// </summary>
        
/// <param name="iniPath">配置文件的路径</param>
        public IniConfig(string iniPath)
        {
            
//请不要删除以下信息
            
//版权:http://XingFuStar.cnblogs.com

            
this.IniPath = iniPath;
        }
public string IniPath
        {
            
set 
            { 
                iniPath 
= value;
                isConfig 
= OnIniPataChanged();
            }
        }
/// <summary>
        
/// 读取Ini中的配置
        
/// </summary>
        
/// <param name="section">节点</param>
        
/// <param name="key"></param>
        
/// <param name="value">返回的键值</param>
        
/// <returns>读取是否成功</returns>
        public bool ReadConfig(string section, string key,ref string value)
        {
            
bool isRead = false;
            
try
            {
                
if (isConfig)
                {
                    
for (int i = 0; i < propertyList.Count; i++)
                    {
                        Property p 
= (Property)propertyList[i];
                        
if (p.Section == section && p.Key == key)
                        {
                            value 
= p.Value;
                            isRead 
= true;
                            
break;
                        }
                    }
                }
            }
            
catch(Exception ex)
            {
                Debug.Print(ex.Message);
            }
            
return isRead;
        }
/// <summary>
        
/// 向INI中写入配置
        
/// </summary>
        
/// <param name="section">节点</param>
        
/// <param name="key"></param>
        
/// <param name="value">要写入的新键值</param>
        
/// <returns>写入是否成功</returns>
        public bool WriteConfig(string section, string key, string value)
        {
            
bool isWrite = false;
            
try
            {
                
if (isConfig)
                {
                    
for (int i = 0; i < propertyList.Count; i++)
                    {
                        Property p 
= (Property)propertyList[i];
                        
if (p.Section == section && p.Key == key)
                        {
                            p.Value 
= value;
                            isWrite 
= SaveIni();
                            
break;
                        }
                    }
                }
            }
            
catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            
return isWrite;
        }
#region 私有方法
        
private bool OnIniPataChanged()
        {
            
bool isLoad = false;
            
try
            {
                
if (File.Exists(iniPath))
                {
                    isLoad 
= LoadIni();
                }
            }
            
catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            
return isLoad;
        }
/// <summary>
        
/// 从文件中加载INI配置信息到列表
        
/// </summary>
        
/// <returns>加载是否成功</returns>
        private bool LoadIni()
        {
            
bool isLoad = false;
            
try
            {
                propertyList 
= new ArrayList();
                StreamReader stream 
= new StreamReader(iniPath, System.Text.Encoding.Default);
                
string section = "";
                
while (stream.Peek() != -1)
                {
                    
string str = stream.ReadLine().Trim();
                    
//判断该行是否有数据
                    if (str.Length > 0)
                    {
                        
//以“;”开头的行为注释行(硬性规定)
                        if (str.Substring(01!= ";")
                        {
                            
//以“[“开头的行为Section行(硬性规定)
                            if (str.Substring(01== "[")
                            {
                                
//记录当前Section
                                section = str.Substring(1, str.IndexOf("]"- 1);
                            }
                            
//有“=”的为数据行(硬性规定)
                            if (str.IndexOf("="> 0)
                            {
                                
string[] temp = str.Split('=');
                                
//将该数据行的属性添加到列表
                                propertyList.Add(new Property(section, temp[0].Trim(), temp[1].Trim(), ""));
                            }
                        }
                        
else
                        {
                            
//将注释行的属性添加到列表
                            propertyList.Add(new Property(section, """", str));
                        }
                    }
                    
else
                    {
                        
//为保证格式与加载前相同,因些将空行也加入了列表
                        propertyList.Add(new Property(""""""""));
                    }
                }
                stream.Close();
                isLoad 
= true;
            }
            
catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            
return isLoad;
        }
/// <summary>
        
/// 将列表中的配置信息保存到INI文件
        
/// </summary>
        
/// <returns>保存是否成功</returns>
        private bool SaveIni()
        {
            
bool isSave = false;
            
try
            {
                StreamWriter stream 
= new StreamWriter(iniPath, false, System.Text.Encoding.Default);
                
string section = "";
                
for (int i = 0; i < propertyList.Count; i++)
                {
                    Property p 
= (Property)propertyList[i];
                    
//写入Section
                    if (p.Section != "" && p.Section != section)
                    {
                        section 
= p.Section;
                        stream.WriteLine(
"[" + section + "]");
                    }
                    
//写入注释
                    if (p.Description != "")
                    {
                        stream.WriteLine(p.Description);
                    }
                    
//写入键和键值
                    if (p.Key != "")
                    {
                        stream.WriteLine(p.Key 
+ " = " + p.Value);
                    }
                    
//写入空行
                    if (p.Section == "" && p.Description == "")
                    {
                        stream.WriteLine(
"");
                    }
                }
                stream.Close();
                isSave 
= true;
            }
            
catch (Exception ex)
            {
                Debug.Print(ex.Message);
            }
            
return isSave;
        }
/// <summary>
        
/// 内部类:Ini属性
        
/// </summary>
        private class Property
        {
            
string section = "";
            
/// <summary>
            
/// 节点
            
/// </summary>
            public string Section
            {
                
get { return section; }
                
set { section = value; }
            }
            
string key = "";
            
/// <summary>
            
/// 键
            
/// </summary>
            public string Key
            {
                
get { return key; }
                
set { key = value; }
            }
            
string value = "";
            
/// <summary>
            
/// 键值
            
/// </summary>
            public string Value
            {
                
get { return this.value; }
                
set { this.value = value; }
            }
            
string description = "";
            
/// <summary>
            
/// 注释
            
/// </summary>
            public string Description
            {
                
get { return description; }
                
set { description = value; }
            }
            
/// <summary>
            
/// 构造函数
            
/// </summary>
            
/// <param name="section"></param>
            
/// <param name="key"></param>
            
/// <param name="value"></param>
            
/// <param name="description"></param>
            public Property(string section, string key, string value, string description)
            {
                
this.Section = section;
                
this.Key = key;
                
this.Value = value;
                
this.Description = description;
            }
        }
        
#endregion
    }
}

接下来再写个使用注册表的配置类
http://xingfustar.cnblogs.com