C#读取保存INI问题
来源:百度知道 编辑:UC知道 时间:2024/06/23 20:25:00
[aaa]
a=value
然后有以下程序:
<pre>
private void butDis_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
try
{
sr = new StreamReader(openFileDialog1.FileName); ----->这里出现问题
section = sr.ReadLine();
if (section.IndexOf('[') < 0)
throw new Exception();
section = section.Substring(section.IndexOf('[') + 1, section.IndexOf(']') - 1);
iniKey = sr.ReadLine();
iniKey = iniKey.Substring(0, iniKey.IndexOf('=')); ---------------------------->直到这里
this.textFileName.Text = openFileDialog1.FileName;
StringBuilder temp = new StringBuilder(255);
你这样的做法不是很妥
有专用的WIN32API用来操作键值对
WritePrivateProfileString 和 GetPrivateProfileString
需要引用的命名空间为:System.Runtime.InteropServices 和 System.Text
参考以下代码
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Ini
{
/// <summary>
/// Create a New INI file to store or load data
/// </summary>
public class IniFile
{
public string path;
[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 fileP