using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
namespace Write_Data_Test
{
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 string pathname = string.Empty;
public string Path;
//写数据
public void IniWriteValue(string section,string key,string iValue, string filePath)
{
WritePrivateProfileString(section, key, iValue, filePath);
}
public Form1(string path)
{
this.Path =path;
}
private void button1_Click(object sender, EventArgs e)
{
//打开文件
/*OpenFileDialog file1 = new OpenFileDialog();
file1.Filter = "文本文件|*.png";
if (file1.ShowDialog()==DialogResult.OK)
{
// StreamReader sr = File.OpenText(file1.FileName);
/* while(sr.EndOfStream!=true)
{
output(sr.ReadLine());
}*/
//pictureBox1.
//加载图片
OpenFileDialog file = new OpenFileDialog();
file.InitialDirectory = ".";
file.Filter = "所有文件(*.*)|*.*";
file.ShowDialog();
if (file.FileName != string.Empty)
{
try
{
pathname = file.FileName; //获得文件的绝对路径
this.pictureBox1.Load(pathname);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog save = new SaveFileDialog();
save.ShowDialog();
if (save.FileName != string.Empty)
{
pictureBox1.Image.Save(save.FileName);
}
}
private void button3_Click(object sender, EventArgs e)
{
// public void IniWriteValue(string section,string key,string iValue);
WritePrivateProfileString("Light1", "light10", "2001", System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");
/* WritePrivateProfileString("Light1", "light2", "2001", System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");
WritePrivateProfileString("Light1", "light3", "2002", System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");
WritePrivateProfileString("Light1", "light4", "2003", System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");
WritePrivateProfileString("Light2", "light1", "2001", System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");
WritePrivateProfileString("Light2", "light2", "2002", System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");
WritePrivateProfileString("Light2", "light3", "2002", System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");*/
//System.AppDomain.CurrentDomain.BaseDirectory + "Light Control.pdf"
MessageBox.Show("成功写入INI文件!", "信息");
}
private void button4_Click(object sender, EventArgs e)
{
//读取ini文件
StringBuilder temp = new StringBuilder(1024);
//string FileName = textBox1.Text;
//string section = textBox2.Text;
//string key = textBox3.Text;
//GetPrivateProfileString(section, key, "无法读取对应数值!", temp, 1024, System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");
GetPrivateProfileString("Light2", "light3", "无法读取对应数值!", temp, 1024, System.AppDomain.CurrentDomain.BaseDirectory + "data.ini");
//显示读取的数值
textBox4.Text = temp.ToString();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
C# 读写INI文件
最新推荐文章于 2023-02-10 16:28:04 发布