通过C#读取ini文件中的内容
来源:互联网 发布:逆战ak12淘宝多少钱 编辑:程序博客网 时间:2024/06/07 02:08
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace CommFunc
{
class Program
{
static void Main(string[] args)
{
List<string> listSection =
IniFunc.ReadSections("D:\\OMNeT++\\samples\\tokenring\\omnetpp.ini");
//Console.WriteLine("下面是omnetpp.ini文件的所有的段");
for (int i = 0; i < listSection.Count; i++)
{
Console.WriteLine(listSection[i]);
List<string> listKey = IniFunc.ReadKeyValues(listSection[i], "D:\\OMNeT++\\samples\\tokenring\\omnetpp.ini");
Console.WriteLine("");
for (int j = 0; j < listKey.Count; j++)
{
Console.WriteLine(listKey[j]);
}
Console.WriteLine();
}
Console.WriteLine();
}
}
public class IniFunc
{
[DllImport("kernel32")]
public extern static int GetPrivateProfileSectionNames(byte[] buffer, int iLen,
string fileName);
[DllImport("kernel32")]
public extern static int GetPrivateProfileSection(string section, byte[] buffer, int nSize, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string section,string key,string defVal,StringBuilder retVal,int size,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(
string lpAppName,string lpKeyName,int nDefault,string lpFileName);
//获取段名
public static List<string> ReadSections(string filePath)
{
byte[] buffer=new byte[65535];
int rel=GetPrivateProfileSectionNames(buffer,buffer.GetUpperBound(0),filePath);
int iCnt,iPos;
List<string> arrayList=new List<string>();
string tmp;
if(rel>0)
{
iCnt=0;iPos=0;
for(iCnt=0;iCnt<rel;iCnt++)
{
if(buffer[iCnt]==0x00)
{
tmp=System.Text.ASCIIEncoding.Default.GetString(buffer,iPos,iCnt-iPos).Trim();
iPos=iCnt+1;
if(tmp!="")
{ arrayList.Add(tmp);}
}
}
}
return arrayList;
}
//获取指定段名下的所有参数
public static List<string> ReadKeyValues(string section,string filePath)
{
byte[] buffer =new byte[32767];
List<string> list=new List<string>();
int length=GetPrivateProfileSection(section,buffer,buffer.GetUpperBound(0),filePath);
string temp;
int postion=0;
for (int i=0;i<length;i++)
{
if(buffer[i]==0x00)
{
temp=System.Text.ASCIIEncoding.Default.GetString(buffer,postion,i-postion).Trim();
postion=i+1;
if(temp.Length>0)
{list.Add(temp);}
}
}
return list;
}
}
}
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
namespace CommFunc
{
class Program
{
static void Main(string[] args)
{
List<string> listSection =
IniFunc.ReadSections("D:\\OMNeT++\\samples\\tokenring\\omnetpp.ini");
//Console.WriteLine("下面是omnetpp.ini文件的所有的段");
for (int i = 0; i < listSection.Count; i++)
{
Console.WriteLine(listSection[i]);
List<string> listKey = IniFunc.ReadKeyValues(listSection[i], "D:\\OMNeT++\\samples\\tokenring\\omnetpp.ini");
Console.WriteLine("");
for (int j = 0; j < listKey.Count; j++)
{
Console.WriteLine(listKey[j]);
}
Console.WriteLine();
}
Console.WriteLine();
}
}
public class IniFunc
{
[DllImport("kernel32")]
public extern static int GetPrivateProfileSectionNames(byte[] buffer, int iLen,
string fileName);
[DllImport("kernel32")]
public extern static int GetPrivateProfileSection(string section, byte[] buffer, int nSize, string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(
string section,string key,string defVal,StringBuilder retVal,int size,string filePath);
[DllImport("kernel32")]
private static extern int GetPrivateProfileInt(
string lpAppName,string lpKeyName,int nDefault,string lpFileName);
//获取段名
public static List<string> ReadSections(string filePath)
{
byte[] buffer=new byte[65535];
int rel=GetPrivateProfileSectionNames(buffer,buffer.GetUpperBound(0),filePath);
int iCnt,iPos;
List<string> arrayList=new List<string>();
string tmp;
if(rel>0)
{
iCnt=0;iPos=0;
for(iCnt=0;iCnt<rel;iCnt++)
{
if(buffer[iCnt]==0x00)
{
tmp=System.Text.ASCIIEncoding.Default.GetString(buffer,iPos,iCnt-iPos).Trim();
iPos=iCnt+1;
if(tmp!="")
{ arrayList.Add(tmp);}
}
}
}
return arrayList;
}
//获取指定段名下的所有参数
public static List<string> ReadKeyValues(string section,string filePath)
{
byte[] buffer =new byte[32767];
List<string> list=new List<string>();
int length=GetPrivateProfileSection(section,buffer,buffer.GetUpperBound(0),filePath);
string temp;
int postion=0;
for (int i=0;i<length;i++)
{
if(buffer[i]==0x00)
{
temp=System.Text.ASCIIEncoding.Default.GetString(buffer,postion,i-postion).Trim();
postion=i+1;
if(temp.Length>0)
{list.Add(temp);}
}
}
return list;
}
}
}
- 通过C#读取ini文件中的内容
- c#读取ini文件
- c#读取ini文件
- C#读取INI文件
- C#读取INI文件
- c#读取ini文件
- c#读取ini文件
- C# 读取ini 文件
- c#读取INI文件
- C#读取ini文件
- C#读取INI文件
- C#读取INI文件
- 读取Config.ini文件内容
- C# .net 读取INI文件 ini文件
- c# 读写ini文件内容
- (转)C# 读取ini文件
- 通过C# OLDEB读取excel2007中的内容(IDE为VS2010)
- 读取INI配置文件内容(头文件head)
- 第三次上机报告
- iOS UIWebView浏览器控件的简单实现
- MFC中的String Table
- Java中的System类
- Visual Studio 2010——C#的ProgressBar和TrackBar控件的使用
- 通过C#读取ini文件中的内容
- 1005 湫湫系列故事——减肥记II
- Android 制作可独立运行的Android模拟器(2.2,2.3亲测可行)
- 排序算法之冒泡排序:一种优化的冒泡排序算法
- C语言程序设计第三次课堂作业
- 三角形类(2)
- ASP.NET 程序 配置IIS “PC-Computer\ASPNET” 登录失败
- Java 里面方法的形式;
- 【easyUI】<div class="easyui-panel" >