首页 > 代码库 > WebClient实现下载txt文件并与用户输入进行匹配

WebClient实现下载txt文件并与用户输入进行匹配

  /// <summary>        /// 验证        /// </summary>        /// <param name="sender"></param>        /// <param name="e"></param>        private void button1_Click(object sender, EventArgs e)        {            //WebClient client = new WebClient();            //byte[] buffer = client.DownloadData("http://ygb.nankai.edu.cn/liangshiyiyou/aaa.txt");            //string res = System.Text.ASCIIEncoding.ASCII.GetString(buffer);            //string f = Encoding.Default.GetString(buffer);                    //if (s == this.textBox1.Text)            //{            //    MessageBox.Show("验证成功!", "系统提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);            //    this.Close();            //}            DownloadFile();            //这样一个TXT就存在一个STR字符串数组里了,str[0]是第一行数据,一次类推。            if (File.Exists("D:\\aaa.txt"))            {                string[] str = File.ReadAllLines("D:\\aaa.txt");                for (int i = 1; i < str.Length; i++)                {                    if (str[i] == this.textBox1.Text)                    {                        MessageBox.Show("验证成功!", "系统提示 ", MessageBoxButtons.OK, MessageBoxIcon.Information);                    }                                                      }                File.Delete(@"D:\\aaa.txt");            }                          }        public void DownloadFile()        {            /// 下载服务器文件至客户端(不带进度条)              /// </summary>              /// <param name="strUrlFilePath">要下载的Web服务器上的文件地址(全路径 如:http://www.dzbsoft.com/test.rar</param>              /// <param name="Dir">下载到的目录(存放位置,机地机器文件夹)</param>              /// <returns>True/False是否上传成功</returns>              string strUrlFilePath = "http://ygb.nankai.edu.cn/liangshiyiyou/aaa.txt";            string strLocalDirPath = "D:/";            // 创建WebClient实例              WebClient client = new WebClient();            //被下载的文件名              string fileName = strUrlFilePath.Substring(strUrlFilePath.LastIndexOf("/"));            //另存为的绝对路径+文件名              string Path = strLocalDirPath + fileName;            try            {                WebRequest myWebRequest = WebRequest.Create(strUrlFilePath);            }            catch (Exception exp)            {                MessageBox.Show("文件下载失败:" + exp.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            try            {                client.DownloadFile(strUrlFilePath, Path);                MessageBox.Show("文件下载成功:", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }            catch (Exception exp)            {                MessageBox.Show("文件下载失败:" + exp.Message, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);            }        }

 

WebClient实现下载txt文件并与用户输入进行匹配