• C#创建并读取ini文件


    1,首先导入命名空间:using System.Runtime.InteropServices;

    2,声明API函数

            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);

    3,创建ini文件

                WritePrivateProfileString("MyQQ", "ID", "798033502", @"C:\QQ.ini");
                WritePrivateProfileString("MyQQ", "PWD", "**********", @"C:\QQ.ini");

    4,声明API函数

            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);

    5,读取ini文件

               StringBuilder temp = new StringBuilder();
                GetPrivateProfileString("MyQQ", "ID", "账号错误", temp, 255, @"C:\QQ.ini");
                string ID = temp.ToString();
                GetPrivateProfileString("MyQQ", "PWD", "读取出错", temp, 255, @"C:\QQ.ini");
                string PWD = temp.ToString();
                MessageBox.Show(ID + ":" + PWD);



    6完整代码如下:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;

    namespace WindowsFormsApplication1
    {
        public partial class Main : Form
        {
            public Main()
            {
                InitializeComponent();
            }
            [DllImport("kernel32")]
            private static extern long WritePrivateProfileString(string section, string key, string val, string filePath);
            private void button1_Click(object sender, EventArgs e)
            {
                WritePrivateProfileString("MyQQ", "ID", "798033502", @"C:\QQ.ini");
                WritePrivateProfileString("MyQQ", "PWD", "**********", @"C:\QQ.ini");
            }
            [DllImport("kernel32")]
            private static extern int GetPrivateProfileString(string section, string key, string def, StringBuilder retVal, int size, string filePath);
           
            private void btnRead_Click(object sender, EventArgs e)
            {
                StringBuilder temp = new StringBuilder();
                GetPrivateProfileString("MyQQ", "ID", "账号错误", temp, 255, @"C:\QQ.ini");
                string ID = temp.ToString();
                GetPrivateProfileString("MyQQ", "PWD", "读取出错", temp, 255, @"C:\QQ.ini");
                string PWD = temp.ToString();
                MessageBox.Show(ID + ":" + PWD);
            }
        }
    }

     

    实例下载:http://download.csdn.net/detail/mypc2010/4426985


  • 相关阅读:
    Win10 rocketmq 安装
    Vulnhub DC7
    Vulnhub DC6
    Vulnhub DC5
    Vulnhub DC4
    Vulnhub DC3
    一文带你学会CSRF漏洞
    Shiro反序列化漏洞复现与实战
    DataPipeline CPO 陈雷:实时数据融合之法:便捷可管理
    DataPipeline合伙人 & CPO 陈雷:企业实时数据管理问题与实践 | 附PPT下载
  • 原文地址:https://www.cnblogs.com/secbook/p/2654870.html
Copyright © 2020-2023  润新知