C# 读写INI文件

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)
        {

        }
        }
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值