Exemplo n.º 1
0
        private void WriteLogFile()
        {
            try
            {
                if (list_log.Count > 0)
                {
                    var name_file = string.Format("Tracert-{0:yyMMddHHmmss}-【{1}】.log", DateTime.Now, this.txt_DomainOrIP.Text.Trim());
                    var path_file = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, name_file);

                    lock (locker)
                    {
                        var file = new FileInfo(path_file);
                        if (file.Exists &&
                            file.Attributes == FileAttributes.ReadOnly)
                        {
                            MessageBox.Show(string.Format("{0} 已存在,且文件只读无法写入日志", name_file));
                            return;
                        }

                        using (var sw = new StreamWriter(path_file, append: true, encoding: Encoding.UTF8))
                        {
                            sw.Write(StringExtension.Join(list_log.ToArray(), string.Empty));
                            sw.Flush();
                            sw.Dispose();
                        }
                    }

                    SyncContext.Send(this.AppendResult,
                                     string.Format("日志文件:{0}", path_file));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Exemplo n.º 2
0
        private void btn_Begin_Click(object sender, EventArgs e)
        {
            this.EnableControl(false);
            list_log.Clear();

            #region 参数校验

            var list_err   = new List <string>();
            var domain     = this.txt_DomainOrIP.Text.Trim();
            var icmp_param = new ICMP_PARAM {
                PingOptions = new PingOptions(1, false)
            };
            if (IPAddress.TryParse(domain, out icmp_param.IPAddress))
            {
                SyncContext.Send(this.AppendResult,
                                 string.Format("正在跟踪到 {0} 间的路由:", icmp_param.IPAddress.ToString()));
            }
            else
            {
                // 解析域名
                var regEx     = new Regex("\\d+\\.\\d+\\.\\d+\\.\\d+"); // 最多4级域名
                var hostEntry = default(IPHostEntry);

                try
                {
                    hostEntry = Dns.GetHostEntry(this.txt_DomainOrIP.Text);
                    foreach (var ipAddr in hostEntry.AddressList)
                    {
                        if (regEx.IsMatch(ipAddr.ToString()))
                        {
                            icmp_param.IPAddress = ipAddr;
                            break;
                        }
                    }

                    if (icmp_param.IPAddress == null)
                    {
                        list_err.Add("域名错误或无法解析");
                    }
                    else
                    {
                        SyncContext.Send(this.AppendResult,
                                         string.Format("正在跟踪到 {0} [{1}] 间的路由:", domain, icmp_param.IPAddress.ToString()));
                    }
                }
                catch (Exception ex)
                {
                    list_err.Add(ex.Message);
                }
            }

            if (list_err.Count > 0)
            {
                this.AppendResult(StringExtension.Join(list_err.ToArray(), ";"));
                WriteLogFile();
                this.EnableControl(true);
                return;
            }

            #endregion 参数校验

            thread = new Thread(new ParameterizedThreadStart(Icmp_Ping))
            {
                IsBackground = true
            };
            thread.Start(icmp_param);
        }