码迷,mamicode.com
首页 > Windows程序 > 详细

C#对config.ini文件进行读取和修改

时间:2020-02-06 15:05:07      阅读:145      评论:0      收藏:0      [点我收藏+]

标签:his   builder   ifile   color   temp   import   目录   str   write   

C#对config.ini文件进行读取和修改:

public partial class Patrolcar : Form之后可以加入如下类:

#region
        public class IniFile
        {
            public string map_length, map_width, maxnum_connect, net_ip;
            public string path;
            [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);

            public IniFile(string INIPath)
            {
                path = INIPath;
            }

            public void getIni()
            {
                map_length = IniReadValue("setting", "map_length").ToString();
                map_width = IniReadValue("setting", "map_width").ToString();
                maxnum_connect = IniReadValue("setting", "maxnum_connect").ToString();
                net_ip = IniReadValue("setting", "net_ip").ToString();
            }

            public void IniWriteValue(string Section, string Key, string Value)
            {
                WritePrivateProfileString(Section, Key, Value, this.path);
            }

            public string IniReadValue(string Section, string Key)
            {
                StringBuilder temp = new StringBuilder(1024);
                int i = GetPrivateProfileString(Section, Key, "", temp, 1024, this.path);
                return temp.ToString();
            }
        }

        IniFile dgd_ini = new IniFile(System.IO.Path.Combine(Application.StartupPath, "config.ini"));
        #endregion

然后在InitializeComponent();语句之后就可以进行函数操作了:

InitializeComponent();
dgd_ini.getIni();

config.ini文件位于...\bin\Debug目录下。

C#对config.ini文件进行读取和修改

标签:his   builder   ifile   color   temp   import   目录   str   write   

原文地址:https://www.cnblogs.com/ming-4/p/12268431.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!