Пример #1
0
        /// <summary>
        /// 从服务器获取hosts文件
        /// </summary>
        /// <param name="URL">统一资源定位符</param>
        /// <param name="filename">本地文件路径</param>
        /// <param name="prog">窗体进度条,若无,可为null</param>
        private static void GetFile(string URL, string filename, DownBox prog)
        {
            try
            {
                HttpWebRequest DownLink = (HttpWebRequest)HttpWebRequest.Create(URL);
                DownLink.Timeout = 10000;       //请求超时时间10s

                HttpWebResponse Source = (HttpWebResponse)DownLink.GetResponse();
                long totalBytes = Source.ContentLength;
                if (totalBytes == -1) totalBytes = 2 * 1024 * 1024;     //若无法获取文件大小,设默认为2MB

                Stream StreamTotal = Source.GetResponseStream();
                Stream StreamOut = new FileStream(filename, FileMode.Create);
                long totalDownloadedByte = 0;
                byte[] by = new byte[1024];
                int OutSize = StreamTotal.Read(by, 0, (int)by.Length);
                while (OutSize > 0)
                {
                    totalDownloadedByte = OutSize + totalDownloadedByte;
                    Application.DoEvents();
                    StreamOut.Write(by, 0, OutSize);
                    if (prog != null)
                    {
                        prog.SetPoint((int)(totalDownloadedByte * 100 / totalBytes));
                    }
                    OutSize = StreamTotal.Read(by, 0, (int)by.Length);
                }
                //完成进度条
                if (prog != null)
                {
                    prog.SetPoint(100);
                }
                SoundPlayer snd = new SoundPlayer(Resources.ding);
                snd.Play();

                StreamOut.Close();
                StreamTotal.Close();
            }
            catch
            {
                //网络错误操作
                msg.SetDialog(3, Resources.msg_err_net);
                msg.ShowDialog();
                throw;
            }
        }
Пример #2
0
        /// <summary>
        /// 根据模式设定下载hosts文件
        /// </summary>
        /// <param name="insmode">选择hosts文件来源:0:本地; 1:Googlecode; 2: Sourceforge; 3: Github</param>
        /// <param name="IPmode">IP协议选择: false:IPv4; true:IPv6</param>
        /// <param name="downForm">下载反馈窗口</param>
        private static void DownFile(int insmode, bool IPmode, DownBox downForm)
        {
            string[] DownPath = new string[4] {
                "", @"http://huhamhire-hosts.googlecode.com/git/downloads/raw/",
                @"http://hosts.huhamhire.com/downfile/raw/",
                @"https://github.com/huhamhire/huhamhire-hosts/raw/master/downloads/raw/"};
            string[] IpPath = new string[2] { @"ipv4", @"ipv6" };

            //初始化路径
            string url;
            string localPath = Path.Combine(Environment.CurrentDirectory, @"resources");

            if (insmode > 0)
            {
                url = DownPath[insmode] + IpPath[(IPmode ? 1 : 0)] + @"_win_ansi/hosts";
                localPath = Path.Combine(localPath, (IpPath[(IPmode ? 1 : 0)] + @"_win_ansi"), @"hosts");
                if (downForm != null)
                {
                    downForm.SetPoint(0);
                }
                GetFile(url, localPath, downForm);

                //对来自GoogleCode的文件转码
                if (insmode == 1)
                    ReEncode(localPath);
                return;
            }
            else
                return;
        }