因为我要根据读取的情况,作出下列的写法:using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Text.RegularExpressions;namespace WinTest
{
public partial class Form3 : Form
{
private string replaceValue = null;
private string iniKey = string.Empty;
private StreamReader sr = null;
private string section = string.Empty;
public Form3()
{
InitializeComponent();
} 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);
int i = GetPrivateProfileString(section, iniKey, "", temp, 255, this.textFileName.Text);
this.textDis.Text = temp.ToString();
}
catch (Exception ex)
{
MessageBox.Show("This isn't legal ini file!");
return;
}
}
} private void butModify_Click(object sender, EventArgs e)
{
if (this.textFileName.Text.Equals(""))
MessageBox.Show("沒有信息", "information");
else if (this.textFileName.Text.Equals(""))
MessageBox.Show("沒讀取文件!", "information");
else
{
WritePrivateProfileString(section, iniKey, this.textDis.Text, this.textFileName.Text);
MessageBox.Show("成功寫入!", "information");
}
}
[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);
private void button1_Click(object sender, EventArgs e)
{
try
{
iniKey = sr.ReadLine();
if (iniKey.IndexOf('[') < 0)
{
iniKey = iniKey.Substring(0, iniKey.IndexOf('=')); StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, iniKey, "", temp, 255, this.textFileName.Text);
this.textDis.Text = temp.ToString();
}
else
{
section = iniKey.Substring(iniKey.IndexOf('[') + 1, iniKey.IndexOf(']') - 1);
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, iniKey, "", temp, 255, this.textFileName.Text);
}
}
catch (Exception ex)
{
MessageBox.Show("at the end!");
}
}
}
}
但现在却保存不了,到底为什么呢?以前我直接在函数里指定section和key的时候是可以,为什么现在这样写就保存不了呢?
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;
using System.Text.RegularExpressions;namespace WinTest
{
public partial class Form3 : Form
{
private string replaceValue = null;
private string iniKey = string.Empty;
private StreamReader sr = null;
private string section = string.Empty;
public Form3()
{
InitializeComponent();
} 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);
int i = GetPrivateProfileString(section, iniKey, "", temp, 255, this.textFileName.Text);
this.textDis.Text = temp.ToString();
}
catch (Exception ex)
{
MessageBox.Show("This isn't legal ini file!");
return;
}
}
} private void butModify_Click(object sender, EventArgs e)
{
if (this.textFileName.Text.Equals(""))
MessageBox.Show("沒有信息", "information");
else if (this.textFileName.Text.Equals(""))
MessageBox.Show("沒讀取文件!", "information");
else
{
WritePrivateProfileString(section, iniKey, this.textDis.Text, this.textFileName.Text);
MessageBox.Show("成功寫入!", "information");
}
}
[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);
private void button1_Click(object sender, EventArgs e)
{
try
{
iniKey = sr.ReadLine();
if (iniKey.IndexOf('[') < 0)
{
iniKey = iniKey.Substring(0, iniKey.IndexOf('=')); StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, iniKey, "", temp, 255, this.textFileName.Text);
this.textDis.Text = temp.ToString();
}
else
{
section = iniKey.Substring(iniKey.IndexOf('[') + 1, iniKey.IndexOf(']') - 1);
StringBuilder temp = new StringBuilder(255);
int i = GetPrivateProfileString(section, iniKey, "", temp, 255, this.textFileName.Text);
}
}
catch (Exception ex)
{
MessageBox.Show("at the end!");
}
}
}
}
但现在却保存不了,到底为什么呢?以前我直接在函数里指定section和key的时候是可以,为什么现在这样写就保存不了呢?
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货