Пример #1
0
        /// <summary>
        /// 保存网络设置信息
        /// </summary>
        /// <param name="networkInfo"></param>
        public static void SaveNetworkConfig(NetworkInfo networkInfo)
        {
            var config_file = GetNetworkConfigFile();

            if (!string.IsNullOrEmpty(config_file))
            {
                ConfigFile config          = new ConfigFile(config_file);
                var        ip_mark_gateway = string.Format("{0}/{1},{2}", networkInfo.IPAddress,
                                                           NetworkConfig.GetSubnetMarkString(NetworkConfig.SubnetMarkValue(networkInfo.SubnetMark)),
                                                           networkInfo.Gateway);

                var ip_address = config.SettingGroups["ipv4"].Settings;
                ip_address["dns"].SetValue(networkInfo.DNS);
                ip_address["address1"].SetValue(ip_mark_gateway);
                config.Save(config_file);
            }
        }
Пример #2
0
        /// <summary>
        /// 读取服务器配置
        /// </summary>
        /// <returns></returns>
        public static NetworkInfo ReadNetWorkConfig()
        {
            NetworkInfo networkInfo = new NetworkInfo();
            var         config_file = GetNetworkConfigFile();

            try
            {
                if (!string.IsNullOrEmpty(config_file))
                {
                    ConfigFile config = new ConfigFile(config_file);

                    var setting_group = config.SettingGroups;
                    var ip_info       = setting_group["ipv4"].Settings;
                    networkInfo.DNS = ip_info["dns"].GetValue();

                    string address_info = ip_info["address1"].GetValue();
                    var    address      = address_info.Split(new[] { '/' }, StringSplitOptions.RemoveEmptyEntries);
                    networkInfo.IPAddress = address[0];

                    var gateway = address[1].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
                    networkInfo.SubnetMark = NetworkConfig.GetSubnetMarkString(int.Parse(gateway[0]));
                    networkInfo.Gateway    = gateway[1];

                    if (setting_group.ContainsKey("ethernet"))
                    {
                        ip_info = setting_group["ethernet"].Settings;
                        if (ip_info.ContainsKey("mac-address"))
                        {
                            networkInfo.MacAddress = ip_info["mac-address"].GetValue();
                        }
                    }
                }
            }
            catch (Exception)
            {
            }
            return(networkInfo);
        }