C#中如何将数据(数组元素)存到以.ini 结尾的文件中去????谢谢了``

yayusuhua 2007-11-04 02:14:46
C#``创建一个window窗体,在窗体中增加点,用数组存点,那么请问如何把点存到.ini的文件中去呢???
...全文
304 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
shiyulun1984 2007-11-05
  • 打赏
  • 举报
回复
上面的代码实现了“打开程序在上次关闭的位置”的功能,参照即可
shiyulun1984 2007-11-05
  • 打赏
  • 举报
回复
调用windows API :
private void Form1_Load(object sender, System.EventArgs e)

{

Ini ini=new Ini("c:\\loc.ini");
Point p=new Point();

if(ini.ReadKey("Loction","X")!=""||ini.ReadKey("Loction","Y")!="")
{
p.X=int.Parse(ini.ReadKey("Loction","X"));
p.Y=int.Parse(ini.ReadKey("Loction","Y"));

}
this.Location=p;



}
private void Form1_Closed(object sender, System.EventArgs e)
{
Ini ini=new Ini("c:\\loc.ini");
ini.WriteKey("Loction","X",this.Location.X.ToString());
ini.WriteKey("Loction","Y",this.Location.Y.ToString());

}
以下是引用了API的ini操作类:
using System;

using System.IO;

using System.Runtime.InteropServices;

//因为我们需要调用API函数,所以必须创建System.Runtime.InteropServices 命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。

using System.Text;

namespace Ini

{

public class IniFile

{

public string path; //INI文件名

[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文件的API函数



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(255);

int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path);

return temp.ToString();

}

//读取INI文件指定

}

}
lextm 2007-11-04
  • 打赏
  • 举报
回复
.NET 没有很好的操作ini的类型,不如直接存到文本文件中。或者XML文件。

111,114

社区成员

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

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

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