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