/// <summary>
        /// 读取INI文件中指定KEY的字符串型值
        /// </summary>
        /// <param name="iniFile">Ini文件</param>
        /// <param name="section">节点名称</param>
        /// <param name="key">键名称</param>
        /// <param name="defaultValue">如果没此KEY所使用的默认值</param>
        /// <returns>读取到的值</returns>
        public static string INIGetStringValue(string iniFile, string section, string key, string defaultValue)
        {
            string    value = defaultValue;
            const int SIZE  = 1024 * 10;

            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException("必须指定节点名称", "section");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("必须指定键名称(key)", "key");
            }

            StringBuilder sb            = new StringBuilder(SIZE);
            uint          bytesReturned = INIOperation.GetPrivateProfileString(section, key, defaultValue, sb, SIZE, iniFile);

            if (bytesReturned != 0)
            {
                value = sb.ToString();
            }
            sb = null;

            return(value);
        }
        private void Test()
        {
            string file = "ip_settings.ini";

            //写入/更新键值
            INIOperation.INIWriteValue(file, "Desktop", "Color", "Red");
            INIOperation.INIWriteValue(file, "Desktop", "Width", "3270");

            INIOperation.INIWriteValue(file, "Toolbar", "Items", "Save,Delete,Open");
            INIOperation.INIWriteValue(file, "Toolbar", "Dock", "True");

            //写入一批键值
            INIOperation.INIWriteItems(file, "Menu", "File=文件\0View=视图\0Edit=编辑");

            //获取文件中所有的节点
            string[] sections = INIOperation.INIGetAllSectionNames(file);

            //获取指定节点中的所有项
            string[] items = INIOperation.INIGetAllItems(file, "Menu");

            //获取指定节点中所有的键
            string[] keys = INIOperation.INIGetAllItemKeys(file, "Menu");

            //获取指定KEY的值
            string value = INIOperation.INIGetStringValue(file, "Desktop", "color", "");

            //删除指定的KEY
            INIOperation.INIDeleteKey(file, "desktop", "color");

            //删除指定的节点
            INIOperation.INIDeleteSection(file, "desktop");

            //清空指定的节点
            INIOperation.INIEmptySection(file, "toolbar");
        }
        /// <summary>
        /// 在INI文件中,删除指定节点中的所有内容。
        /// </summary>
        /// <param name="iniFile">INI文件</param>
        /// <param name="section">节点</param>
        /// <returns>操作是否成功</returns>
        public static bool INIEmptySection(string iniFile, string section)
        {
            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException("必须指定节点名称", "section");
            }

            return(INIOperation.WritePrivateProfileSection(section, string.Empty, iniFile));
        }
        /// <summary>
        /// 在INI文件中,删除指定节点中的指定的键。
        /// </summary>
        /// <param name="iniFile">INI文件</param>
        /// <param name="section">节点</param>
        /// <param name="key">键</param>
        /// <returns>操作是否成功</returns>
        public static bool INIDeleteKey(string iniFile, string section, string key)
        {
            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException("必须指定节点名称", "section");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("必须指定键名称", "key");
            }

            return(INIOperation.WritePrivateProfileString(section, key, null, iniFile));
        }
        /// <summary>
        /// 在INI文件中,将指定的键值对写到指定的节点,如果已经存在则替换
        /// </summary>
        /// <param name="iniFile">INI文件</param>
        /// <param name="section">节点,如果不存在此节点,则创建此节点</param>
        /// <param name="items">键值对,多个用\0分隔,形如key1=value1\0key2=value2</param>
        /// <returns></returns>
        public static bool INIWriteItems(string iniFile, string section, string items)
        {
            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException("必须指定节点名称", "section");
            }

            if (string.IsNullOrEmpty(items))
            {
                throw new ArgumentException("必须指定键值对", "items");
            }

            return(INIOperation.WritePrivateProfileSection(section, items, iniFile));
        }
Esempio n. 6
0
        private void Button5_Click(object sender, EventArgs e)
        {
            string sectionname;

            if (treeView1.SelectedNode != null)
            {
                sectionname = treeView1.SelectedNode.Text;
                treeView1.SelectedNode.Remove();
                INIOperation.INIDeleteSection(file, sectionname);
                treeView1.SelectedNode = null;
                MessageBox.Show("方案【" + sectionname + "】删除成功!");
            }
            else
            {
                MessageBox.Show("请选择要删除的方案名称!");
            }
        }
        /// <summary>
        /// 在INI文件中,指定节点写入指定的键及值。如果已经存在,则替换。如果没有则创建。
        /// </summary>
        /// <param name="iniFile">INI文件</param>
        /// <param name="section">节点</param>
        /// <param name="key">键</param>
        /// <param name="value">值</param>
        /// <returns>操作是否成功</returns>
        public static bool INIWriteValue(string iniFile, string section, string key, string value)
        {
            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException("必须指定节点名称", "section");
            }

            if (string.IsNullOrEmpty(key))
            {
                throw new ArgumentException("必须指定键名称", "key");
            }

            if (value == null)
            {
                throw new ArgumentException("值不能为null", "value");
            }

            return(INIOperation.WritePrivateProfileString(section, key, value, iniFile));
        }
        /// <summary>
        /// 获取INI文件中指定节点(Section)中的所有条目的Key列表
        /// </summary>
        /// <param name="iniFile">Ini文件</param>
        /// <param name="section">节点名称</param>
        /// <returns>如果没有内容,反回string[0]</returns>
        public static string[] INIGetAllItemKeys(string iniFile, string section)
        {
            string[]  value = new string[0];
            const int SIZE  = 1024 * 10;

            if (string.IsNullOrEmpty(section))
            {
                throw new ArgumentException("必须指定节点名称", "section");
            }

            char[] chars         = new char[SIZE];
            uint   bytesReturned = INIOperation.GetPrivateProfileString(section, null, null, chars, SIZE, iniFile);

            if (bytesReturned != 0)
            {
                value = new string(chars).Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
            }
            chars = null;

            return(value);
        }
Esempio n. 9
0
        private void InitTree_FromConfig()
        {
            //TreeNode node = new TreeNode("base1");
            //TreeNode node2 = new TreeNode("base2");
            //treeView1.Nodes.Add(node);
            //treeView1.Nodes.Add(node2);
            //node.Nodes.Add("a");
            //node.Nodes.Add("b");
            //node.Nodes.Add("c");
            //node2.Nodes.Add("d");
            //node2.Nodes.Add("e");
            //node2.Nodes.Add("f");
            LogHelper.Debug(this.GetType(), "开始读取配置文件中配置方案信息。。。");
            string sectionsname = "";
            string groupname    = "";

            string[]        sections   = INIOperation.INIGetAllSectionNames(file);
            List <string>   group      = new List <string>();
            List <TreeNode> group_node = new List <TreeNode>();
            TreeNode        node       = null;
            int             count;

            for (int i = 0; i < sections.Length; i++)
            {
                sectionsname = sections[i];
                LogHelper.Debug(this.GetType(), "设置父节点名称为“当前配置”。。。");
                groupname = INIOperation.INIGetStringValue(file, sectionsname, "group", "当前配置");
                if (group.IndexOf(groupname) < 0)
                {
                    group.Add(groupname);
                    node = new TreeNode(groupname);
                    treeView1.Nodes.Add(node);
                    group_node.Add(node);
                }
                count = group.IndexOf(groupname);
                group_node[count].Nodes.Add(sectionsname);
            }
            LogHelper.Debug(this.GetType(), "完成读取配置文件中配置方案信息。。。");
            treeView1.Sort();
        }
Esempio n. 10
0
        /// <summary>
        /// 获取INI文件中指定节点(Section)中的所有条目(key=value形式)
        /// </summary>
        /// <param name="iniFile">Ini文件</param>
        /// <param name="section">节点名称</param>
        /// <returns>指定节点中的所有项目,没有内容返回string[0]</returns>
        public static string[] INIGetAllItems(string iniFile, string section)
        {
            //返回值形式为 key=value,例如 Color=Red
            uint MAX_BUFFER = 32767;        //默认为32767

            string[] items = new string[0]; //返回值

            //分配内存
            IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));

            uint bytesReturned = INIOperation.GetPrivateProfileSection(section, pReturnedString, MAX_BUFFER, iniFile);

            if (!(bytesReturned == MAX_BUFFER - 2) || (bytesReturned == 0))
            {
                string returnedString = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned);
                items = returnedString.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
            }

            Marshal.FreeCoTaskMem(pReturnedString);     //释放内存

            return(items);
        }
Esempio n. 11
0
        /// <summary>
        /// 读取INI文件中指定INI文件中的所有节点名称(Section)
        /// </summary>
        /// <param name="iniFile">Ini文件</param>
        /// <returns>所有节点,没有内容返回string[0]</returns>
        public static string[] INIGetAllSectionNames(string iniFile)
        {
            uint MAX_BUFFER = 32767;           //默认为32767

            string[] sections = new string[0]; //返回值

            //申请内存
            IntPtr pReturnedString = Marshal.AllocCoTaskMem((int)MAX_BUFFER * sizeof(char));
            uint   bytesReturned   = INIOperation.GetPrivateProfileSectionNames(pReturnedString, MAX_BUFFER, iniFile);

            if (bytesReturned != 0)
            {
                //读取指定内存的内容
                string local = Marshal.PtrToStringAuto(pReturnedString, (int)bytesReturned).ToString();

                //每个节点之间用\0分隔,末尾有一个\0
                sections = local.Split(new char[] { '\0' }, StringSplitOptions.RemoveEmptyEntries);
            }

            //释放内存
            Marshal.FreeCoTaskMem(pReturnedString);

            return(sections);
        }
Esempio n. 12
0
        private void Button4_Click(object sender, EventArgs e)
        {
            string sectionname = null;

            if (treeView1.SelectedNode != null)
            {
                sectionname = treeView1.SelectedNode.Text;
            }
            string str        = null;
            string txtIP      = textBox1.Text;
            string txtSubMark = textBox2.Text;
            string txtGateWay = textBox3.Text;
            string txtDNS1    = textBox4.Text;
            string txtDNS2    = textBox5.Text;
            //string NetworkDescription = comboBox1.Text;
            string NetWork_Name = comboBox1.SelectedItem.ToString();

            string[]         arr = NetWork_Name.Split('|');
            Boolean          Str_return = true;
            string           txtIpRadio, txtDnsRadio;
            NetWorkOperation NetWork = new NetWorkOperation();

            if (sectionname != null && sectionname != "当前配置")
            {
                str = sectionname;
            }
            else if (sectionname == null || sectionname == "当前配置")
            {
                str = Interaction.InputBox("请输入方案名称!\n\n如方案名称已存在,则会覆盖以前的方案!", "新方案", "", -1, -1);
            }
            if (str == null || str == "")
            {
                MessageBox.Show("方案名称不能为空!");
                return;
            }

            if (radioButton2.Checked)
            {
                txtIpRadio = "1";
                Str_return = NetWork.IP_Check(txtIP, txtSubMark, txtGateWay);
                if (!Str_return)
                {
                    return;
                }
                INIOperation.INIWriteValue(file, str, "networkdescription", arr[2]);
                INIOperation.INIWriteValue(file, str, "ipradio", txtIpRadio);
                INIOperation.INIWriteValue(file, str, "iplist", txtIP);
                INIOperation.INIWriteValue(file, str, "masklist", txtSubMark);
                INIOperation.INIWriteValue(file, str, "gatewaylist", txtGateWay);
            }
            else
            {
                txtIpRadio = "0";
                INIOperation.INIWriteValue(file, str, "networkdescription", arr[2]);
                INIOperation.INIWriteValue(file, str, "ipradio", txtIpRadio);
            }
            if (radioButton4.Checked)
            {
                txtDnsRadio = "1";
                Str_return  = NetWork.Dns_Check(txtDNS1, txtDNS2);
                if (!Str_return)
                {
                    return;
                }

                INIOperation.INIWriteValue(file, str, "dnsradio", txtDnsRadio);
                INIOperation.INIWriteValue(file, str, "preferreddnslist", txtDNS1);
                INIOperation.INIWriteValue(file, str, "optionaldnslist", txtDNS2);
            }
            else
            {
                txtDnsRadio = "0";
                INIOperation.INIWriteValue(file, str, "dnsradio", txtDnsRadio);
            }
            treeView1.Nodes.Clear();
            InitTree_FromConfig();
            this.treeView1.ExpandAll();
            MessageBox.Show("方案【" + str + "】保存成功!");
        }
Esempio n. 13
0
        //选择树的节点,触发事件
        private void TreeView1_MouseDown(object sender, MouseEventArgs e)
        {
            //选择树的节点并点击右键,触发事件
            if (e.Button == MouseButtons.Right)//判断你点的是不是右键
            {
                Point    ClickPoint  = new Point(e.X, e.Y);
                TreeNode CurrentNode = treeView1.GetNodeAt(ClickPoint);
                if (CurrentNode != null && true == CurrentNode.Checked) //判断你点的是不是一个节点
                {
                    switch (CurrentNode.Name)                           //根据不同节点显示不同的右键菜单,当然你可以让它显示一样的菜单
                    {
                    case "":
                        //右键菜单
                        CurrentNode.ContextMenuStrip = contextMenuStrip1;
                        break;

                    default:
                        break;
                    }
                }
                treeView1.SelectedNode = CurrentNode;//选中这个节点
            }
            //选择树的节点并点击左键,触发事件
            if (e.Button == MouseButtons.Left)
            {
                if ((sender as TreeView) != null)
                {
                    treeView1.SelectedNode = treeView1.GetNodeAt(e.X, e.Y);
                    if (treeView1.SelectedNode != null)
                    {
                        string sectionname = treeView1.SelectedNode.Text;
                        //设置点击当前配置时自动读取当前使用的IP设置信息
                        if (sectionname == "当前配置")
                        {
                            LogHelper.Debug(this.GetType(), "“当前配置”获取IP配置信息。。。");
                            NetWorkOperation NetWork      = new NetWorkOperation();
                            string           NetWork_Name = comboBox1.SelectedItem.ToString();
                            string[]         arr          = NetWork_Name.Split('|');

                            int DhcpCount = NetWork.Query_NetWork_Dhcp_Status(arr[2]);

                            if (DhcpCount > 0)
                            {
                                radioButton1.Checked = true;
                                radioButton2.Checked = false;
                                radioButton3.Checked = true;
                                radioButton4.Checked = false;
                                textBox1.Enabled     = false;
                                textBox2.Enabled     = false;
                                textBox3.Enabled     = false;
                                textBox4.Enabled     = false;
                                textBox5.Enabled     = false;
                            }
                            else
                            {
                                List <string> list = NetWork.GetNetWork(arr[2]);
                                radioButton1.Checked = false;
                                radioButton2.Checked = true;
                                radioButton3.Checked = false;
                                radioButton4.Checked = true;
                                textBox1.Enabled     = true;
                                textBox2.Enabled     = true;
                                textBox3.Enabled     = true;
                                textBox4.Enabled     = true;
                                textBox5.Enabled     = true;
                                textBox1.Text        = list[0];
                                textBox2.Text        = list[1];
                                textBox3.Text        = list[2];
                                textBox4.Text        = list[1];
                                textBox5.Text        = list[2];
                            }
                            return;
                        }

                        LogHelper.Debug(this.GetType(), "读取配置文件信息到IP栏并展示。。。");
                        string ipradio            = INIOperation.INIGetStringValue(file, sectionname, "ipradio", "");
                        string dnsradio           = INIOperation.INIGetStringValue(file, sectionname, "dnsradio", "");
                        string iplist             = INIOperation.INIGetStringValue(file, sectionname, "iplist", "");
                        string masklist           = INIOperation.INIGetStringValue(file, sectionname, "masklist", "");
                        string gatewaylist        = INIOperation.INIGetStringValue(file, sectionname, "gatewaylist", "");
                        string preferreddnslist   = INIOperation.INIGetStringValue(file, sectionname, "preferreddnslist", "");
                        string optionaldnslist    = INIOperation.INIGetStringValue(file, sectionname, "optionaldnslist", "");
                        string networkdescription = INIOperation.INIGetStringValue(file, sectionname, "networkdescription", "");
                        string NetWorkName        = "";
                        for (int i = 0; i < comboBox1.Items.Count; i++)
                        {
                            NetWorkName = comboBox1.Items[i].ToString();
                            string[] arr = NetWorkName.Split('|');
                            if (arr[2] == networkdescription)
                            {
                                comboBox1.SelectedIndex = comboBox1.Items.IndexOf(NetWorkName);
                                break;
                            }
                        }

                        if (ipradio != "")
                        {
                            if (ipradio == "1")
                            {
                                radioButton1.Checked = false;
                                radioButton2.Checked = true;
                                textBox1.Enabled     = true;
                                textBox2.Enabled     = true;
                                textBox3.Enabled     = true;
                            }
                            else
                            {
                                radioButton1.Checked = true;
                                radioButton2.Checked = false;
                                textBox1.Enabled     = false;
                                textBox2.Enabled     = false;
                                textBox3.Enabled     = false;
                            }
                            if (dnsradio == "1")
                            {
                                radioButton3.Checked = false;
                                radioButton4.Checked = true;
                                textBox4.Enabled     = true;
                                textBox5.Enabled     = true;
                            }
                            else
                            {
                                radioButton3.Checked = true;
                                radioButton4.Checked = false;
                                textBox4.Enabled     = false;
                                textBox5.Enabled     = false;
                            }
                            textBox1.Text = iplist;
                            textBox2.Text = masklist;
                            textBox3.Text = gatewaylist;
                            textBox4.Text = preferreddnslist;
                            textBox5.Text = optionaldnslist;
                        }
                    }
                }
            }
        }