private void Form1_Load(object sender, EventArgs e) { String ddnsStr = ConfigAppSettings.GetValue("ddns"); if (!string.IsNullOrEmpty(ddnsStr)) { //加载初始化地址 this.textBox1.Text = ddnsStr; } string autoRefreshStr = ConfigAppSettings.GetValue("autoRefresh"); if (!string.IsNullOrEmpty(autoRefreshStr)) { this.autoRefresh.Checked = Convert.ToBoolean(autoRefreshStr); }//if if (this.timer.Enabled) { this.button1.Text = "停止"; } string startMini = ConfigAppSettings.GetValue("startMini"); if (!string.IsNullOrEmpty(startMini)) { this.startMini.Checked = Convert.ToBoolean(startMini); }//if RegistryKey reg = null; try { if (!System.IO.File.Exists(exeFilePath)) { MessageBox.Show("文件路径异常" + exeFilePath, "提示"); } reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run", true); if (reg == null) { reg = Registry.LocalMachine.CreateSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVersion\Run"); } string autoStartStr = reg.GetValue(exeName)?.ToString(); this.autoStart.Checked = exeFilePath.Equals(autoStartStr); } catch (Exception ex) { MessageBox.Show(ex.Message + ex.StackTrace, "读取注册表异常"); } finally { if (reg != null) { reg.Close(); } } //先同步一下 SynTimer_Tick(sender, e); }//method
private void DdnsTaskFun(object obj) { //OutLog("开始更新DDNS"); txtDate = DateTime.Now.ToString("yy/MM/dd HH:mm"); string ipStr = ""; string ip6Str = GetIpv6(); string addressStr = ""; string beforeIp = txtIp; string ipJson = ""; try { ipJson = new HttpClient().GetStringAsync("http://ip.chinaz.com/getip.aspx").Result; } catch (Exception ex) { } Match match = new Regex("ip:'(?<ip>.*?)',address:'(?<address>.*?)'").Match(ipJson); if (!match.Success) { OutLog("查询外网ip失败:" + ipJson); OutLog("将直接访问DDNS 让动态域名服务商自动识别ip"); txtIp = "auto"; txtAddress = ipJson; } else { ipStr = match.Groups["ip"].Value; addressStr = match.Groups["address"].Value; if (!string.IsNullOrEmpty(ipStr) && TxtIp.Equals(ipStr) && count < 10) { count++; return; } txtIp = ipStr; txtAddress = addressStr; } if (!txtIp.Equals(beforeIp)) { OutLog(beforeIp + "->" + txtIp); } string urls = ConfigAppSettings.GetValue("ddns"); if (string.IsNullOrEmpty(urls)) { OutLog("ddns路径未配置!"); return; } List <string> urlArr = urls.Split(new string[] { "\r\n" }, StringSplitOptions.None).ToList(); foreach (string pathStr in urlArr) { if (!string.IsNullOrEmpty(pathStr)) { ChangeIp(pathStr, ipStr, ip6Str); } } count = 0; }//method
static void Main() { Process[] processes = System.Diagnostics.Process.GetProcessesByName(Application.CompanyName); if (processes.Length > 1) { MessageBox.Show("应用程序已经在运行中。。"); Thread.Sleep(1000); System.Environment.Exit(1); } else { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); System.Resources.ResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(Form1)); //任务 DdnsTask task = new DdnsTask(); //定时器 System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += (sender, e) => { //任务 task.DdnsStart(); }; timer.Enabled = false; timer.AutoReset = true; //一直执行 timer.Interval = 5 * 60 * 1000; //间隔毫秒 //是否自动启动任务 string autoRefreshStr = ConfigAppSettings.GetValue("autoRefresh"); if (!string.IsNullOrEmpty(autoRefreshStr)) { bool autoRefreshBool = Convert.ToBoolean(autoRefreshStr); if (autoRefreshBool) { timer.Start(); } }//if //是否自动后台运行 string startMini = ConfigAppSettings.GetValue("startMini"); bool isShow = true; if (!string.IsNullOrEmpty(startMini)) { bool startMiniBool = Convert.ToBoolean(startMini); if (startMiniBool) { isShow = false; } }//if if (isShow) { main = new Form1(timer, task); main.Show(); } ToolStripMenuItem appout = new ToolStripMenuItem(); appout.Name = "退出ToolStripMenuItem"; appout.Text = "退出"; appout.Click += (sender, e) => { if (MessageBox.Show("是否确认退出程序?", "退出", MessageBoxButtons.OKCancel, MessageBoxIcon.Question) == DialogResult.OK) { // 关闭所有的线程 if (main != null) { main.Dispose(); main.Close(); } System.Environment.Exit(0); } }; ContextMenuStrip notifyMenu = new ContextMenuStrip(); notifyMenu.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { appout }); notifyMenu.Name = "contextMenuStrip1"; notifyMenu.Size = new System.Drawing.Size(101, 48); NotifyIcon notifyIcon = new NotifyIcon(); notifyIcon.ContextMenuStrip = notifyMenu; notifyIcon.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); notifyIcon.Text = "动态域名解析"; notifyIcon.Visible = true; notifyIcon.MouseDoubleClick += (sender, e) => { if (main == null || main.IsDisposed) { main = new Form1(timer, task); main.Show(); } }; Application.Run(); } //else } //method
}//method private void TextBox1_TextChanged(object sender, EventArgs e) { ConfigAppSettings.SetValue("ddns", this.textBox1.Text); }
private void StartMini_CheckedChanged(object sender, EventArgs e) { ConfigAppSettings.SetValue("startMini", Convert.ToString(this.startMini.Checked)); }
private void AutoRefresh_CheckedChanged(object sender, EventArgs e) { ConfigAppSettings.SetValue("autoRefresh", Convert.ToString(this.autoRefresh.Checked)); }