Windows Mobile 读写配置文件

读写INI:

 首先,得到当前exe的运行路径  
  ApplicationPath   =   Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase);  
  下面是操作INI文件的类  

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

namespace  Ini
ExpandedBlockStart.gifContractedBlock.gif
{
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**////   <summary>   
    
///   Ini的摘要说明。   
    
///   </summary>   

    public class IniFile
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{

        
public string path;

ContractedSubBlock.gifExpandedSubBlockStart.gif       
写入信息文件#region   写入信息文件
        
private void WritePrivateProfileString(string ApplicationName, string KeyName, string Value, string FileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string[] iniItems = new string[0];
            
string iniLines = null;
            
string iniLine = null;
            
string iniItem = null;
            
int i, j;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//读取INI文件;   
                System.IO.StreamReader iniFile = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
                iniLines 
= iniFile.ReadToEnd();
                iniFile.Close();
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
            }


            
//如果信息文件还沒有任何內容,將节点写到第一行   
            if (iniLines == null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                StreamWriter CreatFile 
= File.CreateText(FileName);
                CreatFile.Close();
                StreamWriter FileWriter 
= new StreamWriter(FileName, true, System.Text.Encoding.Default);
                FileWriter.WriteLine(
"[" + ApplicationName + "]");
                FileWriter.WriteLine(KeyName 
+ "=" + Value);
                FileWriter.Close();
                
return;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//否则的话得到已有的节点   
                iniItems = System.Text.RegularExpressions.Regex.Split(iniLines, "\r\n");
            }


            
//以回车符分割,得到每一行   
            bool HaveParentNode = false;
            
string IniContent = "";
            
int IndexLength = 0;
            
if (iniItems[iniItems.Length - 1== "")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                IndexLength 
= iniItems.Length - 1;
            }

            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                IndexLength 
= iniItems.Length;
            }

            
for (i = 0; i < IndexLength; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                iniItem 
= iniItems[i].Trim();
                
if (iniItem[0== '[' && iniItem[iniItem.Length - 1== ']')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
if (IniContent == "")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        IniContent 
= iniItems[i].ToString();
                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        IniContent 
+= "\r\n" + iniItems[i].ToString();
                    }

                    
if (iniItems[i].ToString() == "[" + ApplicationName + "]")
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        HaveParentNode 
= true;
                        
bool HaveNode = false;
                        
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
//找到相对应的父节点的话,查找是否有对应的子节点   
                            for (j = (i + 1); j < IndexLength; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                iniLine 
= iniItems[j].Trim();
                                
if (iniLine[0== '[' && iniLine[iniLine.Length - 1== ']')
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    
if (!HaveNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif                                    
{
                                        HaveNode 
= true;
                                        IniContent 
+= "\r\n" + KeyName + "=" + Value;
                                    }

                                    
break;
                                }


                                iniLine 
= iniItems[j].TrimStart().Replace("   """);
                                
if (iniLine.Substring(0, Math.Min(KeyName.Length + 1, iniLine.Length)).ToUpper() == KeyName.ToUpper() + "=")
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    
//如果找到了Key匹配   
                                    HaveNode = true;
                                    IniContent 
+= "\r\n" + KeyName + "=" + Value;
                                }

                                
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                                
{
                                    IniContent 
+= "\r\n" + iniItems[j].ToString();
                                }

                            }

                        }

                        
catch (System.Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            ex.ToString();
                        }


                        
if (!HaveNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            HaveNode 
= true;
                            IniContent 
+= "\r\n" + KeyName + "=" + Value;
                        }

                    }

                    
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        
for (j = (i + 1); j < IndexLength; j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            iniLine 
= iniItems[j].Trim();
                            
if (iniLine[0== '[' && iniLine[iniLine.Length - 1== ']')
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                
break;
                            }

                            
else
ExpandedSubBlockStart.gifContractedSubBlock.gif                            
{
                                IniContent 
+= "\r\n" + iniItems[j].ToString();
                            }

                        }

                    }

                }

            }


            
if (!HaveParentNode)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                IniContent 
+= "\r\n[" + ApplicationName + "]";
                IniContent 
+= "\r\n" + KeyName + "=" + Value;
            }


            StreamWriter ReplaceFile 
= File.CreateText(FileName);
            ReplaceFile.Close();
            StreamWriter ReplaceWriter 
= new StreamWriter(FileName, true, System.Text.Encoding.Default);
            ReplaceWriter.Write(IniContent);
            ReplaceWriter.Close();
        }

        
#endregion


ContractedSubBlock.gifExpandedSubBlockStart.gif        
读取信息文件#region   读取信息文件
        
private string GetPrivateProfileString(string ApplicationName, string KeyName, string Default, string FileName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
string[] iniItems = new string[0];
            
string iniLines;
            
string iniLine;
            
int i, j;
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//读取INI文件;   
                System.IO.StreamReader iniFile = new System.IO.StreamReader(FileName, System.Text.Encoding.Default);
                iniLines 
= iniFile.ReadToEnd();
                iniFile.Close();
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return Default;
            }

            
//以回车符分割,得到每一行   
            iniItems = System.Text.RegularExpressions.Regex.Split(iniLines, "\r\n"); ;
            
//遍历每一行   
            for (i = 0; i < iniItems.GetLength(0); i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//找到匹配值   
                if (iniItems[i].Trim().ToUpper() == '[' + ApplicationName.Trim().ToUpper() + ']')
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
//从下一行开始搜索   
                    for (j = i + 1; j < iniItems.GetLength(0); j++)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        iniLine 
= iniItems[j].Trim();
                        
if (iniLine.Length > 0)
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
//如果找到了另一个段,那么就越段了,则返回默认值   
                            if (iniLine[0== '[' && iniLine[iniLine.Length - 1== ']'return Default;
                        }

                        
//去掉所有空格   
                        iniLine = iniItems[j].TrimStart().Replace("   """);
                        
if (iniLine.Substring(0, Math.Min(KeyName.Length + 1, iniLine.Length)).ToUpper() == KeyName.ToUpper() + "=")
ExpandedSubBlockStart.gifContractedSubBlock.gif                        
{
                            
//如果找到了Key匹配   
                            return iniItems[j].Substring(iniItems[j].IndexOf('='+ 1);
                        }

                    }

                    
return Default;//没有找到key匹配的,则返回默认值   
                }

            }

            
return Default;//返回默认值   
        }

        
#endregion


        
public IniFile(string INIPath)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            path 
= INIPath;
        }


        
public void IniWriteValue(string Section, string Key, string Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            WritePrivateProfileString(Section, Key, Value, 
this.path);
        }


        
public string IniReadValue(string Section, string Key)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return GetPrivateProfileString(Section, Key, ""this.path);
        }


    }

}

使用方法  
  写信息文件  
  IniFile   NewIni   =   new   IniFile(ApplicationPath   +   "\\Test.ini");  
  NewIni.IniWriteValue("测试信息","测试一","1");  
   
  读取  
  IniFile   NewIni   =   new   IniFile(ApplicationPath   +   "\\Test.ini");  
  string   a   =   NewIni   .IniReadValue("测试信息","测试一");  
   
  读取结果  
  a   =   1

 备注:以上代码在HP   2490(WM5)上测试通过

 

 


 

 

转载于:https://www.cnblogs.com/greatandforever/archive/2009/11/04/1595794.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值