比如INI的内容如下
[code=INIFile]
[a]
aaa
bbb
ccc
ddd
[b]
aaa
bbb
ccc
eee
[o]
320990
jfoekljds
f23iklfds
9832lfds[/code]这里没有key=value 直接就是内容然后要把[a]下面的内容读取到listbox1里
然后要把[b]下面的内容读取到listbox2里
然后要把[c]下面的内容读取到listbox3里
..... 等这样如何写呢?
[code=INIFile]
[a]
aaa
bbb
ccc
ddd
[b]
aaa
bbb
ccc
eee
[o]
320990
jfoekljds
f23iklfds
9832lfds[/code]这里没有key=value 直接就是内容然后要把[a]下面的内容读取到listbox1里
然后要把[b]下面的内容读取到listbox2里
然后要把[c]下面的内容读取到listbox3里
..... 等这样如何写呢?
普通的文件格式,自己写程序,因为有规律,肯定可以写出来的
比如按行读,遇到空行不处理,遇到"["符号开始处理listbox1,遇到下一个"["开始处理listbox2.
可以一次读出一段的内容,然后用 \0 分割
[DllImport("kernel32", EntryPoint = "GetPrivateProfileSectionW", CharSet = CharSet.Unicode)]
public static extern int GetPrivateProfileSection(
string lpApplicationName,
byte [] lpReturnedString,
int nSize,
string lpFileName);////////////////////////////
/// <summary>
/// 配置段的全部文本
/// (配置项为Name=Value格式 |
/// 多个配置项之间用单个 \n (0x0A) 分隔 |
/// 整个文本以连续两个\n结束 )
/// </summary>
/// <res>
/// </res>
public string Text {
get {
byte[] buffer = new byte[8192];
int readed = 0;
readed=INIConfiguration.GetPrivateProfileSection(
_Name,
buffer,
8192,
_Configuration.Path
);
if (readed <= 0) return "";
string strOut = "";
//将 \0 转换成 \n
//Unicode字符串,双字节,高位是0
for (int i = 0; i < readed; i+=2)
if (buffer[i] == 0 && buffer[i+1]==0) buffer[i] = 0x0A;
strOut = Encoding.Unicode.GetString(buffer, 0, readed*2);
return strOut;
}
set {
string vValue = value.Replace("\r\n", "\n");
vValue = vValue.Replace("\r", "\n");
if (vValue.Length < 2) vValue = "\n\n";
if (!vValue.EndsWith("\n\n")) { //不是两个回车结尾,整理一下
if (!vValue.EndsWith("\n"))
vValue += "\n\n";
else
vValue += "\n";
}
byte[] buffer = Encoding.Unicode.GetBytes(vValue);
//将\n转换成\0,同样Unicode字符串时双字符
for (int i = 0; i < buffer.Length; i += 2)
if (buffer[i] == 0x0A && buffer[i + 1] == 0x00)
buffer[i] = 0x00;
INIConfiguration.WritePrivateProfileSection(
_Name,
buffer,
_Configuration.Path
);
}
}
string[] content = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries);
reader.Close();
List<List<stirng>> result = new List<List<string>>();
int index = -1;
if(!content[0].StartWith("["))
{
index ++;
result.Add(new List<string>());
}
foreach(string s in content)
{
if(s.StartWith("["))
{
result.Add(new List<string>());
index ++;
}
else
{
result[index].Add(s);
}
}
在回复框里写的,这样应该能做的到了
string[] content = reader.ReadToEnd().Split(new string[] { "\r\n" }, StringSplitOptions.RemoveEmptyEntries); //从文件里把所有文本读出来,并按分行符分割成字符串数组
reader.Close();
List <List <stirng>> result = new List <List <string>>();
int index = -1;
if(!content[0].StartWith("[")) //如果第一行不是section名称,而先创建一个list,并把index指向这个list
{
index ++;
result.Add(new List <string>());
}
foreach(string s in content) //循环这个数组,相当于一行一行的读取数据
{
if(s.StartWith("[")) //如果这行数据是以[开头,则说明开始一个新的section了,则添加一个新的list,并将index,指向这个list,否则将数据添加到index指向的list
{
result.Add(new List <string>());
index ++;
}
else
{
result[index].Add(s);
}
}
最后得到的结果就是每个section存储到不同的list里了,不就是你要的结果吗?
我添加了说是不能数据转换..
不过我最后是把
else
{
result[index].Add(s);
} 改成else
{
if (s.Contains("a"))
{
listbox1.Items.Add(s);
}
else
{
if (s.Contains("b"))
{
listbox2.Items.Add(s);
}
}
}
我是先检查这个字符串中有没有对应的字符..然后再加入到相应的listbox不知怎么把[a]下面的内容加到listbox1里 [b]里的内容listbox2里....等其他也一样!谢谢!