WebClient實現下載txt檔案並與使用者輸入進行匹配

來源:互聯網
上載者:User

標籤:style   blog   http   io   color   ar   os   for   sp   

  /// <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檔案並與使用者輸入進行匹配

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.