public static void Save( ServiceConfigInfo info ) { RegistryKey key = null; try { key = Registry.LocalMachine.CreateSubKey(REG_PATH); if (info.DirectoryIP != IPAddress.None) { key.SetValue(DIRECTORY_IP_VALUE_NAME,IPAddress.HostToNetworkOrder( BitConverter.ToInt32( info.DirectoryIP.GetAddressBytes(), 0) ), RegistryValueKind.DWord); } if (info.Port != 0) { key.SetValue(DIRECTORY_PORT_VALUE_NAME, info.Port); } if (!string.IsNullOrEmpty(info.Web)) { key.SetValue(WEB_VALUE_NAME, "http://" + info.DirectoryIP.ToString() + "/" + info.Web + WEB_URL); } } catch (Exception) { throw; } finally { if (key != null) { key.Close(); } } }
private void btnSave_Click(object sender, RoutedEventArgs e) { ServiceConfigInfo serverInfo = new ServiceConfigInfo(); if (string.IsNullOrEmpty(txtIP.Text.Trim())) { MessageBox.Show("目录服务IP地址不能为空。"); return; } else { IPAddress ip = IPAddress.None; bool rightFormat = IPAddress.TryParse(txtIP.Text.Trim(),out ip); if ( rightFormat ) { serverInfo.DirectoryIP = ip; } else { MessageBox.Show( "目录服务IP地址格式不正确。" ); return; } } if ( string.IsNullOrEmpty( txtPort.Text.Trim() ) ) { MessageBox.Show( "目录服务端口不能为空。" ); return; } else { int port = 0; bool rightFormat = int.TryParse( txtPort.Text.Trim(), out port ); if ( rightFormat ) { serverInfo.Port = port; } else { MessageBox.Show( "目录服务端口格式不正确。" ); return; } } if ( string.IsNullOrEmpty( txtWeb.Text.Trim() ) ) { MessageBox.Show( "虚拟目录不能为空。" ); return; } else { serverInfo.Web = txtWeb.Text.Trim(); } try { RegUtil.Save(serverInfo); MessageBox.Show( "保存成功,重启服务生效。" ); } catch (Exception ex) { MessageBox.Show( "保存失败; " +ex.Message ); } XMLUtil.Save( _configInfo ); }
public static ServiceConfigInfo Load() { ServiceConfigInfo info = new ServiceConfigInfo(); info.DirectoryIP = IPAddress.None; info.Port = 0; RegistryKey key = null; try { key = Registry.LocalMachine.OpenSubKey(REG_PATH, false); } catch (Exception) { if (key != null) { key.Close(); } return info; }; if ( key == null ) { return info; } try { string ip = key.GetValue(DIRECTORY_IP_VALUE_NAME).ToString(); //info.DirectoryIP = new IPAddress( long.Parse(ip) ); info.DirectoryIP = IPAddress.Parse( ip ); } catch (Exception ex) { // Log.Debug(ex.Message); } try { string port = key.GetValue(DIRECTORY_PORT_VALUE_NAME).ToString(); info.Port = int.Parse(port); } catch (Exception ex) { //Log.Debug(ex.Message); } try { string url = key.GetValue(WEB_VALUE_NAME).ToString(); if (!string.IsNullOrEmpty(url)) { url = url.Replace("http://", "").Replace("Http://", "").Replace(WEB_URL, ""); url = url.Substring(url.IndexOf("/") + 1); if (!string.IsNullOrEmpty(url)) { info.Web = url; } } } catch (Exception ex) { //Log.Debug(ex.Message); } if (key != null ) { key.Close(); } return info; }