再次请教关于.ini文件的生成和读取的问题

herolucy 2005-04-07 07:45:25
在一个页面中,有三个文本框,我希望在这个页面中,把三个框中的值存入一个.ini文件中.然后在第二的页面上,再从这个.ini文件里把三个值读出来.请教各位,该如何实现.
...全文
212 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
herolucy 2005-04-07
  • 打赏
  • 举报
回复
多谢各位帮忙,分数不多,表表心意
conan19771130 2005-04-07
  • 打赏
  • 举报
回复
using System;
using System.IO;
using System.Text;
using System.Runtime.InteropServices;
using System.Collections;
using System.ComponentModel;
using System.Data;


namespace com.zggps

{

public class WinIni
{
struct ERRORS
{
static public string ERROR_FILE_NOT_EXIST="文件不存在";
}
private string m_strFile; //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函数


/// <summary>
/// ini文件构造函数,错误抛出
/// </summary>
/// <param name="strFile">文件路径,以\开头表示绝对路径,否则为相对路径</param>
public WinIni(string strFile)
{
string strAppPath=GetAppPath();
if(strFile.Substring(0,1)!="\\")
{
if(strAppPath=="\\")
m_strFile="\\"+strFile;
else
m_strFile=strAppPath+"\\"+strFile;
}
else
m_strFile=strFile;
System.IO.FileInfo fi=new FileInfo(m_strFile);
if(!fi.Exists)
throw(new Exception(ERRORS.ERROR_FILE_NOT_EXIST));
}
/// <summary>
/// ini文件构造函数,带默认值,默认为应用程序目录下config.ini,错误抛出
/// </summary>
public WinIni()
{
string strAppPath=GetAppPath();
if(strAppPath=="\\")
m_strFile="\\config.ini";
else
m_strFile=strAppPath+"\\config.ini";
System.IO.FileInfo fi=new FileInfo(m_strFile);
if(!fi.Exists)
throw(new Exception(ERRORS.ERROR_FILE_NOT_EXIST));

}
/// <summary>
/// 得到当前应用程序路径
/// </summary>
public static string GetAppPath()
{
string strTmp=System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);

return strTmp.Substring(6,strTmp.Length-6);
}


/// <summary>
/// 写配置文件
/// </summary>
/// <param name="Section">节</param>
/// <param name="Key">键</param>
/// <param name="Value">值</param>
public void WriteProFileString(string Section,string Key,string Value)
{

try
{
WritePrivateProfileString(Section,Key,Value,m_strFile);
}
catch(Exception err)
{
throw err;
}
}

/// <summary>
/// 读配置文件
/// </summary>
/// <param name="Section">节</param>
/// <param name="Key">键</param>
/// <returns>值</returns>
public string ReadProFileString(string Section,string Key)
{

try
{
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(Section,Key,"",temp,255,m_strFile);
return temp.ToString();
}
catch(Exception err)
{
throw err;
}

}

}
}
athossmth 2005-04-07
  • 打赏
  • 举报
回复
http://www.codeproject.com/csharp/cs_ini.asp

egxsun 2005-04-07
  • 打赏
  • 举报
回复
现在参数配置大都采用xml了
xuchi 2005-04-07
  • 打赏
  • 举报
回复
写ini文件
[DllImport("kernel32")]
private static extern long WritePrivateProfileString(string section,string key,string val,string filePath);
读ini文件
[DllImport("kernel32")]
private static extern int GetPrivateProfileString(string section,string key,string def,StringBuilder retVal,int size,string filePath);
Flashcom 2005-04-07
  • 打赏
  • 举报
回复
INI的读写比较慢!要调用API,不如用XML的读写,这样会快很多的!现在这些的我都是用XML来保存,代码也比较简单!
yzgnick 2005-04-07
  • 打赏
  • 举报
回复
using System.IO;
using System.Text;
FileStream fs=new FileStream(路徑+文件名.ini,FileMode.OpenOrCreate,FileAccess.Write);
StreamWriter sw=new StreamWriter(fs);
string strValue=TextBox1.Text+TextBox2.Text+TextBox3;
sw.Write(strValue);

StreamReader sr=new StreamReader(fs);
sr.ReadLine ();
lalac 2005-04-07
  • 打赏
  • 举报
回复
it involved in file reading about ini,first you should clear the struct of ini file, and just do it

111,125

社区成员

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

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

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