100分啦!!!!C#如何将程序属性写进.ini文件,如何导入??? http://www.zdnet.com.cn/developer/code/story/0,2000081534,39065664,00.htm 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 [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);///.............. public void IniWriteValue(string Section,string Key,string Value,string filepath)//对ini文件进行写操作的函数 { WritePrivateProfileString(Section,Key,Value,filepath); } public string IniReadValue(string Section,string Key,string filepath)//对ini文件进行读操作的函数 { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp, 255, filepath); return temp.ToString(); } private void button1_Click(object sender, System.EventArgs e) { if(openFileDialog1.ShowDialog()==DialogResult.OK) { this.textBox1 .Text= IniReadValue("ODBC 32 bit Data Sources","MS Access Database",openFileDialog1.FileName); } } private void button2_Click(object sender, System.EventArgs e) { if(saveFileDialog1.ShowDialog()==DialogResult.OK) { IniWriteValue ("ODBC 32 bit Data Sources","MS Access Database",this.textBox1 .Text,saveFileDialog1.FileName); } }读写都在里面了。 http://www.zdnet.com.cn/developer/code/story/0,2000081534,39065664-1,00.htm C#读写INI文件 作者: 盛放Thursday, October 10 2002 11:08 AM 虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。 INI文件是文本文件,由若干节(section)组成,在每个带括号的标题下面,是若干个关键词(key)及其对应的值(Value) [Section] Key=ValueVC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面我介绍一个读写INI文件的C#类并利用该类保存窗体的坐标,当程序再次运行的时候,窗体将显示在上次退出时的位置。INIFILE类:using System;using System.IO;using System.Runtime.InteropServices;因为我们需要调用API函数,所以必须创建System.Runtime.InteropServices命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。using System.Text;namespace Ini{ publicclass IniFile { publicstring path; //INI文件名 [DllImport("kernel32")] privatestaticexternlong WritePrivateProfileString(string section,string key,string val,string filePath); [DllImport("kernel32")] privatestaticexternint GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath); //声明读写INI文件的API函数 public IniFile(string INIPath) { path = INIPath; } //类的构造函数,传递INI文件名 publicvoid IniWriteValue(string Section,string Key,string Value) { WritePrivateProfileString(Section,Key,Value,this.path); } //写INI文件 publicstring IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); return temp.ToString(); } //读取INI文件指定 }} 调用INIFILE类:新建一个标准的C# WINDOWS应用程序项目,在窗体中分别增加命名为sect、key、val的三个文本框。增加如下代码:using Ini; //创建命名空间//当窗体关闭时保存窗体坐标privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { IniFile ini = new IniFile("C:\\test.ini"); ini.IniWriteValue("LOC" ,"x" ,this.Location.X.ToString() ); ini.IniWriteValue("LOC " ,"y" ,this.Location.Y.ToString() ); //ToString方法将数字转换为字符串 }//当窗体启动时,读取INI文件的值并赋值给窗体 privatevoid Form1_Load(object sender, System.EventArgs e) { IniFile ini = new IniFile("C:\\test.ini"); Point p=new Point() ; if ((ini.IniReadValue ("LOC" ,"x" )!="" ) && (ini.IniReadValue ("LOC" ,"y" )!="")) //判断返回值,避免第一次运行时为空出错 { p.X=int.Parse (ini.IniReadValue ("LOC" ,"x" )); p.Y =int.Parse (ini.IniReadValue ("LOC" ,"y" )); // int.Parse将字符串转换为int this.Location =p; } }ccid.net http://www.csdn.net/Develop/Read_Article.asp?Id=17764 using System.IO;然后在事件中写入:private void Button5_Click(object sender, System.EventArgs e){StreamWriter writer = new StreamWriter(@"D:\test.txt");Writer.Write("cloud_scorpion");writer.Flush();writer.Close();} ComBobox的问题,请帮忙 web版wpf开发中关于datagrid操作! 求一个正则表达式 C#中 =! 是什么意思? 一个入门级问题 请教一个C# 开发PDA 程序的小问题,如何得到当前PDA上GSM的状态 再问一次被提过n次问的问题(文本框内容格式保存的问题) c# treeview C#获取Android设备中文件 如何让一个小图片在form里移动。 如何将数组传入方法 明明有了runat="server",却提示必须包含在runat="server"中。
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);
///..............
public void IniWriteValue(string Section,string Key,string Value,string filepath)//对ini文件进行写操作的函数
{
WritePrivateProfileString(Section,Key,Value,filepath);
}
public string IniReadValue(string Section,string Key,string filepath)//对ini文件进行读操作的函数
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,
255, filepath);
return temp.ToString(); } private void button1_Click(object sender, System.EventArgs e)
{
if(openFileDialog1.ShowDialog()==DialogResult.OK)
{
this.textBox1 .Text= IniReadValue("ODBC 32 bit Data Sources","MS Access Database",openFileDialog1.FileName);
}
} private void button2_Click(object sender, System.EventArgs e)
{
if(saveFileDialog1.ShowDialog()==DialogResult.OK)
{
IniWriteValue ("ODBC 32 bit Data Sources","MS Access Database",this.textBox1 .Text,saveFileDialog1.FileName);
}
}
读写都在里面了。
C#读写INI文件 作者: 盛放
Thursday, October 10 2002 11:08 AM 虽然微软早已经建议在WINDOWS中用注册表代替INI文件,但是在实际应用中,INI文件仍然有用武之地,尤其现在绿色软件的流行,越来越多的程序将自己的一些配置信息保存到了INI文件中。
INI文件是文本文件,由若干节(section)组成,在每个带括号的标题下面,是若干个关键词(key)及其对应的值(Value) [Section] Key=ValueVC中提供了API函数进行INI文件的读写操作,但是微软推出的C#编程语言中却没有相应的方法,下面我介绍一个读写INI文件的C#类并利用该类保存窗体的坐标,当程序再次运行的时候,窗体将显示在上次退出时的位置。INIFILE类:using System;using System.IO;using System.Runtime.InteropServices;因为我们需要调用API函数,所以必须创建System.Runtime.InteropServices命名空间以提供可用于访问 .NET 中的 COM 对象和本机 API 的类的集合。using System.Text;namespace Ini{ publicclass IniFile { publicstring path; //INI文件名 [DllImport("kernel32")] privatestaticexternlong WritePrivateProfileString(string section,string key,string val,string filePath); [DllImport("kernel32")] privatestaticexternint GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath); //声明读写INI文件的API函数 public IniFile(string INIPath) { path = INIPath; } //类的构造函数,传递INI文件名 publicvoid IniWriteValue(string Section,string Key,string Value) { WritePrivateProfileString(Section,Key,Value,this.path); } //写INI文件 publicstring IniReadValue(string Section,string Key) { StringBuilder temp = new StringBuilder(255); int i = GetPrivateProfileString(Section,Key,"",temp,255,this.path); return temp.ToString(); } //读取INI文件指定 }}
调用INIFILE类:新建一个标准的C# WINDOWS应用程序项目,在窗体中分别增加命名为sect、key、val的三个文本框。增加如下代码:using Ini; //创建命名空间//当窗体关闭时保存窗体坐标privatevoid Form1_Closing(object sender, System.ComponentModel.CancelEventArgs e) { IniFile ini = new IniFile("C:\\test.ini"); ini.IniWriteValue("LOC" ,"x" ,this.Location.X.ToString() ); ini.IniWriteValue("LOC " ,"y" ,this.Location.Y.ToString() ); //ToString方法将数字转换为字符串 }//当窗体启动时,读取INI文件的值并赋值给窗体 privatevoid Form1_Load(object sender, System.EventArgs e) { IniFile ini = new IniFile("C:\\test.ini"); Point p=new Point() ; if ((ini.IniReadValue ("LOC" ,"x" )!="" ) && (ini.IniReadValue ("LOC" ,"y" )!="")) //判断返回值,避免第一次运行时为空出错 { p.X=int.Parse (ini.IniReadValue ("LOC" ,"x" )); p.Y =int.Parse (ini.IniReadValue ("LOC" ,"y" )); // int.Parse将字符串转换为int this.Location =p; } }
ccid.net
然后在事件中写入:
private void Button5_Click(object sender, System.EventArgs e)
{
StreamWriter writer = new StreamWriter(@"D:\test.txt");
Writer.Write("cloud_scorpion");
writer.Flush();
writer.Close();
}