zoukankan      html  css  js  c++  java
  • C#对Ini文件读写

    命名空间引入  using System.Runtime.InterOpServices;

    创建一个IniFIleHelper类对ini文件进行读写操作

    class IniFileHelper {//Ini文件读写类
    //INI文件名
    public string Path;
    //引用外部非托管代码的kenrnel3
    /// <summary>
    /// 写键值对
    /// </summary>
    /// <param name="section">节点(模块)名称。</param>
    /// <param name="key">Key(键名称)。</param>
    /// <param name="val">读取失败时的默认值</param>
    /// <param name="size">内容缓冲区的长度</param>
    /// <param name="filePath">INI文件名</param>
       [DllImport("kernel32")]
       private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);//filePath为文件存放路径

    /// 读取INI文件中指定的Key的值
    /// </summary>
    /// <param name="section">节点(模块)名称。</param>
    /// <param name="key">Key(键名称)。</param>
    /// <param name="val">读取失败时的默认值</param>
    /// <param name="size">内容缓冲区的长度</param>
    /// <param name="filePath">INI文件名</param>
       [DllImport("kernel32")]
       private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);//filePath为文件存放路径
       public IniFileHelper(string FilePath) {
         this.Path = FilePath;
       }
    //提取写入方法
    /// <summary>
    ///
    /// </summary>
    /// <param name="Secton">节点(模块)</param>
    /// <param name="Key">键(名称)</param>
    /// <param name="Value">值(数值)</param>
        public void IniWriteValue(string Secton,string Key,string Value) {
           WritePrivateProfileString(Secton,Key,Value,Path);//filePath为文件存放路径
        }
    //提取读取方法
    /// <summary>
    ///
    /// </summary>
    /// <param name="Section">节点(模块)</param>
    /// <param name="Key">键(名称)</param>
    /// <returns>返回读取的数据</returns>
    public string IniReadValue(string Section,string Key) {
    StringBuilder temp = new StringBuilder(255);
    int s = GetPrivateProfileString(Section, Key, "", temp, 255, Path);//Path为文件存放路径
    return temp.ToString();
    }

    以下为截图:

    具体引用:

     该窗体的代码:

    namespace DataSourceCopy {
    public partial class Form1 : Form {
    public Form1() {
    InitializeComponent();
    }

    private void groupBox2_Enter(object sender, EventArgs e) {

    }

    private void Form1_Load(object sender, EventArgs e) {

    cmb_BaseType.SelectedIndex = 0;
    cmb_SIdentity.SelectedIndex = 0;
    cmb_TIdentity.SelectedIndex = 0;
    // MessageBox.Show(string.Concat(System.AppDomain.CurrentDomain.BaseDirectory, "Config.ini"));

    GetValue();
    }

    private void GetValue() {
    IniFileHelper ini = new IniFileHelper(string.Concat(System.AppDomain.CurrentDomain.BaseDirectory, "Config.ini"));
    try {
    //连接节点分类
    txb_ConnName.Text = ini.IniReadValue("Connection", "ConnName");
    cmb_BaseType.SelectedItem = ini.IniReadValue("Connection", "BaseType");
    //源数据库分类
    txb_SServer.Text = ini.IniReadValue("Source", "SServer");
    cmb_SIdentity.SelectedItem = ini.IniReadValue("Source", "SIdentity");
    txb_SDataBase.Text = ini.IniReadValue("Source", "SDataBase");
    txb_SUserID.Text = ini.IniReadValue("Source", "SID");
    txb_SPwd.Text = ini.IniReadValue("Source", "SPwd");
    //目标数据库分类
    txb_TServer.Text = ini.IniReadValue("Target", "TServer");
    cmb_TIdentity.SelectedItem = ini.IniReadValue("Target", "TIdentity");
    txb_TDataBase.Text = ini.IniReadValue("Target", "TDataBase");
    txb_TUserID.Text = ini.IniReadValue("Target", "TID");
    txb_TPwd.Text = ini.IniReadValue("Target", "TPwd");

    } catch (Exception) {
    MessageBox.Show("读取失败!");
    }

    }

    private void btn_OK_Click(object sender, EventArgs e) {
    try {
    SetValue();
    MessageBox.Show("保存成功!", "提示", MessageBoxButtons.OK);
    } catch (Exception) {
    throw ;
    }

    }

    private void SetValue() {

    IniFileHelper ini = new IniFileHelper(string.Concat(System.AppDomain.CurrentDomain.BaseDirectory, "Config.ini"));
    try {
    //连接节点分类
    ini.IniWriteValue("Connection", "ConnName", txb_ConnName.Text);
    ini.IniWriteValue("Connection", "BaseType", cmb_BaseType.SelectedText);
    //源数据库分类
    ini.IniWriteValue("Source", "SServer", txb_SServer.Text);
    ini.IniWriteValue("Source", "SIdentity", cmb_SIdentity.SelectedText);
    ini.IniWriteValue("Source", "SDataBase", txb_SDataBase.Text);
    ini.IniWriteValue("Source", "SID", txb_SUserID.Text);
    ini.IniWriteValue("Source", "SPwd", txb_SPwd.Text);
    //目标数据库分类
    ini.IniWriteValue("Target", "TServer", txb_TServer.Text);
    ini.IniWriteValue("Target", "TIdentity", cmb_TIdentity.SelectedItem.ToString());
    ini.IniWriteValue("Target", "TDataBase", txb_TDataBase.Text);
    ini.IniWriteValue("Target", "TID", txb_TUserID.Text);
    ini.IniWriteValue("Target", "TPwd", txb_TPwd.Text);

    } catch (Exception) {

    throw;
    }

    }

    private void button1_Click(object sender, EventArgs e) {
    SetValue();
    MessageBox.Show("已经刷新!");
    }

    private void btn_Change_Click(object sender, EventArgs e) {
    try {
    ChangeValue(txb_SServer, txb_TServer);
    ChangeValue(cmb_SIdentity, cmb_TIdentity);
    ChangeValue(txb_SDataBase, txb_TDataBase);
    ChangeValue(txb_SUserID, txb_TUserID);
    ChangeValue(txb_SPwd, txb_TPwd);
    MessageBox.Show("对调成功");
    } catch (Exception) {
    MessageBox.Show("交换失败!");
    }
    }
    private void ChangeValue(Control box1,Control box2) {
    string txt = box1.Text;
    box1.Text = box2.Text;
    box2.Text = txt;
    }
    }
    }

     截图:

  • 相关阅读:
    Codeforces704C. Black Widow
    Codeforces704B. Ant Man
    暑假学习日记2013/8/5
    暑假学习日记2013/7/31
    暑假学习日记2013/7/26
    暑假学习日记2013/7/25
    暑假学习日记2013/7/24
    暑假学习日记2013/7/23
    线性同余方程的解法笔记
    暑假学习日记2013/7/22
  • 原文地址:https://www.cnblogs.com/Kai-YoungMaster/p/13261721.html
Copyright © 2011-2022 走看看