C#中将数据保存到INI文件

散步的魚 2010-11-10 08:57:59
        public class IniFile
{
public string path;//INI文件名

/*声明读写INI文件的API函数*/
[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文件名*/
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(500);
int i = GetPrivateProfileString(Section, Key, "", temp, 500, this.path);
return temp.ToString();
}
public bool ExistINIFile()
{
return File.Exists(path);
}
}

             if ((readBuffer[0] == 254) && (readBuffer[1] > 4 ) && (checksum1(readBuffer[1]) != 0))//保存每个以254开头的数组
{
IniFile ini = new IniFile("C:\\moto.ini");

for (int i = 0; i < readBuffer[1]; i++)
{
ini.IniWriteValue("", "readBuffer" + i.ToString(), readBuffer[i].ToString());
}
}

现在有两个问题:
1、这样每次只能保存一组数据 而且是固定8个长度
像这样
readBuffer0=254
readBuffer1=8
readBuffer2=18
readBuffer3=16
readBuffer4=14
readBuffer5=0
readBuffer6=0
readBuffer7=202
怎样才能将所有每个以254开头的数组都保存起来呢 判断条件上面实时显示部分和这一样可以正常工作的
            /*实时数据输出*/
if ((readBuffer[0] == 253) && (readBuffer[1] == 34) && (checksum1(readBuffer[1]) != 0))
{
pacurrent = (readBuffer[2] + readBuffer[3] * 256.00) / 1000.00;
textBox10.Text = pacurrent.ToString("f2");

pbcurrent = (readBuffer[4] + readBuffer[5] * 256.00) / 1000.00;
textBox11.Text = pbcurrent.ToString("f2");

pccurrent = (readBuffer[6] + readBuffer[7] * 256.00) / 1000.00;
textBox12.Text = pccurrent.ToString("f2");

mcurrent = (readBuffer[8] + readBuffer[9] * 256.00) / 1000.00;
textBox6.Text = mcurrent.ToString("f2");

bvoltage = (readBuffer[10] + readBuffer[11] * 256.00) / 1000.00;
textBox5.Text = bvoltage.ToString("f2");

cspeed = (readBuffer[14] + readBuffer[15] * 256);
textBox2.Text = cspeed.ToString();

pusage = (readBuffer[18]);
textBox7.Text = pusage.ToString();

temp = (readBuffer[21] + readBuffer[22] * 256);
textBox13.Text = temp.ToString();

cpower = (readBuffer[25] + readBuffer[26] * 256.00) / 1000.00;
textBox3.Text = cpower.ToString("f2");

distance = (readBuffer[29] + readBuffer[30] * 256 + readBuffer[31] * 65536);
textBox8.Text = distance.ToString();
}

请指教

2、怎样将结果保存成每个数组一行的格式
像这样
254,8,18,16,4,0,0,202
254,.................
.
.
.
...全文
930 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
散步的魚 2010-11-10
  • 打赏
  • 举报
回复
            else if ((readBuffer[0] == 254) && (readBuffer[1] > 4 ) && (checksum1(readBuffer[1]) != 0))//保存每个以254开头的数组  
{
//IniFile ini = new IniFile("C:\\param.ini");

for (int i = 0; i < readBuffer[1]; i++)
{
//ini.IniWriteValue("", "readBuffer" + i.ToString(), readBuffer[i].ToString());
File.AppendAllText("c:\\param.txt", readBuffer[i].ToString() + Environment.NewLine);
}
}

改为TXT方法后 保存的格式是这样的
254
8
18
64
31
0
0
137
254
6
18
136
19
79
254
5
18
0
235
254
5
18
0
235
我想改成这样的
254,8,18,64,31,0,0,137 这样的 每个以254开头的放一行 中间用“,”隔开 请问如何做到
flyerwing 2010-11-10
  • 打赏
  • 举报
回复
ini文件就是key=value的形式的。
你说的用普通的txt就可以完成了,干嘛要绕弯使用ini呢?
==================================================
上面说的对呀,INI是分键值的;如果如楼主那么说干脆直接用TXT文件多好呀.,
deyygywxf 2010-11-10
  • 打赏
  • 举报
回复
学习~帮顶!!!给分谢谢
兔子-顾问 2010-11-10
  • 打赏
  • 举报
回复
用xml不是更容易?
XML序列化
http://blog.csdn.net/wuyazhe/archive/2010/07/30/5775666.aspx
散步的魚 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 zhoufoxcn 的回复:]
引用楼主 fishrd 的回复:
C# code
public class IniFile
{
public string path;//INI文件名

/*声明读写INI文件的API函数*/
[DllImport("kernel32")]
private stati……

ini文件就是key=value的形式的。
你说的用普通的txt就可以完成了,干嘛要绕弯使用ini呢?……
[/Quote]
            else if ((readBuffer[0] == 254) && (readBuffer[1] > 4 ) && (checksum1(readBuffer[1]) != 0))//保存每个以254开头的数组
{
//IniFile ini = new IniFile("C:\\moto.ini");

for (int i = 0; i < readBuffer[1]; i++)
{
//ini.IniWriteValue("", "readBuffer" + i.ToString(), readBuffer[i].ToString());
File.AppendAllText("c:\\Param.txt", readBuffer[i].ToString() + Environment.NewLine);
}
}

保存格式是这样了
254
8
18
64
31
0
0
137
254
6
18
136
19
79
254
5
18
0
235
254
5
18
0
235
.
.
.
请问怎样改成每个以254开头的数组成一行 中间用逗号隔开
龍月 2010-11-10
  • 打赏
  • 举报
回复
还是xml吧 操作简单。 ini 没有专门的类操作,只能api 此消息通过 【CSDN论坛 Winform测试版】 回复! 龙月.NET的博客
guyehanxinlei 2010-11-10
  • 打赏
  • 举报
回复
Friendly Up!
jianshao810 2010-11-10
  • 打赏
  • 举报
回复
那时我面试就是这么的题目,我直接就像写txt文件那样,只是后缀改为ini。。晕死,看来要被bs啦
teddy000 2010-11-10
  • 打赏
  • 举报
回复
你是小扯淡还是小蜗牛来着?
要多与周公交流
rimtd0314 2010-11-10
  • 打赏
  • 举报
回复
ddddd
周公 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用楼主 fishrd 的回复:]
C# code
public class IniFile
{
public string path;//INI文件名

/*声明读写INI文件的API函数*/
[DllImport("kernel32")]
private stati……
[/Quote]
ini文件就是key=value的形式的。
你说的用普通的txt就可以完成了,干嘛要绕弯使用ini呢?
xiaowei5780651 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 jianshao810 的回复:]

那时我面试就是这么的题目,我直接就像写txt文件那样,只是后缀改为ini。。晕死,看来要被bs啦
[/Quote]


晕死。。这都可以阿,哈哈,

改value值不是就可以了么,何必那么麻烦阿。
散步的魚 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 16 楼 foxdave 的回复:]
254开头为一行,用三元表达式

C# code
File.AppendAllText("c:\\param.txt", readBuffer[i].ToString() + readBuffer[i].ToString().StartsWith("254")?Environment.NewLine:",");
[/Quote]
错误 12 无法将类型“string”隐式转换为“bool”
散步的魚 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 15 楼 foxdave 的回复:]
周公说得对
保存成一行,把

C# code
Environment.NewLine

换成

C# code
","
[/Quote]
没有实现换行的功能
YUN607 2010-11-10
  • 打赏
  • 举报
回复

菜鸟 来学习的 瞻仰瞻仰!!!
huangwenquan123 2010-11-10
  • 打赏
  • 举报
回复
[Quote=引用 14 楼 huangwenquan123 的回复:]
喔 还要以每个254开头的换行?
if(readBuffer[i].ToString()=="254")
File.AppendAllText("c:\\param.txt","\r\t"+ReadBuffer[i].ToString()+",");
[/Quote]
回车换行是\r\n才对!一直忘了= =!
wangan2008 2010-11-10
  • 打赏
  • 举报
回复
txt转换为ini
Versus1008 2010-11-10
  • 打赏
  • 举报
回复
Justin-Liu 2010-11-10
  • 打赏
  • 举报
回复
254开头为一行,用三元表达式
File.AppendAllText("c:\\param.txt", readBuffer[i].ToString() + readBuffer[i].ToString().StartsWith("254")?Environment.NewLine:",");
Justin-Liu 2010-11-10
  • 打赏
  • 举报
回复
周公说得对
保存成一行,把
Environment.NewLine

换成
","
加载更多回复(2)

111,129

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧