110,538
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Data;
using System.IO;
using System.Text;
public void WriteFile(string content, string fileSavePath)
{
if (System.IO.File.Exists(fileSavePath))
{
System.IO.File.Delete(fileSavePath);
}
using (System.IO.FileStream fs = System.IO.File.Create(fileSavePath))
{
byte[] info = new System.Text.Encoding.GetEncoding("gb2312").GetBytes(content); //防止乱码
fs.Write(info, 0, info.Length);
}
}
public string ReadFile(string fileOpenPath)
{
if (!System.IO.File.Exists(fileOpenPath))
{
return null;
}
using (System.IO.StreamReader sr = System.IO.File.OpenText(fileOpenPath))
{
return sr.ReadToEnd().ToString();
}
}
using System;using System.Drawing;using System.Collections;using System.ComponentModel;using System.Windows.Forms;using System.Data;using System.Runtime.InteropServices;using System.Text;namespace INI3{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } [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) { openFileDialog1.ShowDialog(); textBox1.Text = openFileDialog1.FileName; } private void button2_Click(object sender, EventArgs e) { string FileName = textBox1.Text; string section = textBox2.Text; string key = textBox3.Text; string keyValue = textBox4.Text; WritePrivateProfileString(section, key, keyValue, FileName); MessageBox.Show("成功写入INI文件!", "信息"); } private void button3_Click(object sender, EventArgs e) { StringBuilder temp = new StringBuilder (255) ; string FileName = textBox1.Text; string section = textBox2.Text; string key = textBox3.Text; int i = GetPrivateProfileString(section,key,"无法读取对应数值!",temp,255,FileName); //显示读取的数值 textBox4.Text = temp.ToString(); } }}