/// <summary> /// 下载局域网文件 /// </summary> /// <param name="path">文件路径,如:\\192.168.10.1\app\app\123.zip</param> /// <param name="username">计算机名称</param> /// <param name="password">计算机密码</param> public static bool RequestWindowsShared(string path, string username, string password) { //文件总大小 int allBytesCount = 0; //每次传输大小 int byteTemp = 1024; //当前位置 int bytePosition = 0; //剩下大小 int remain = 0; System.Net.FileWebRequest request = null; System.Net.FileWebResponse response = null; System.IO.Stream stream = null; System.IO.FileStream fileStream = null; try { Uri uri = new Uri(path); request = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(uri); System.Net.ICredentials ic = new System.Net.NetworkCredential(username, password); request.Credentials = ic; response = (System.Net.FileWebResponse)request.GetResponse(); stream = response.GetResponseStream(); byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); string filename = System.Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\" + System.IO.Path.GetFileName(path); fileStream = new FileStream(filename, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite); allBytesCount = bytes.Length; remain = allBytesCount; while (remain > 0) { fileStream.Write(bytes, bytePosition, byteTemp); remain = remain - byteTemp; bytePosition = bytePosition + byteTemp; fileStream.Flush(); if (remain < byteTemp) { byteTemp = remain; } } Console.WriteLine("下载成功!"); return(true); } catch (Exception ex) { Console.WriteLine(ex.Message); return(false); } finally { fileStream.Close(); fileStream.Dispose(); stream.Close(); stream.Dispose(); } }
//下载 private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) { if (textBox2.Text == "") { MessageBox.Show("请选择下载地址", "提示"); return; } try { string targetLoginName = "administrator"; string targetPassword = "******"; string targetIP = "192.168.1.12"; using (BLL.IdentityScope iss = new BLL.IdentityScope(targetLoginName, targetPassword, targetIP)) { filepath = Directory.GetFiles("\\\\192.168.1.12\\力朗共享文件\\POD安装包"); filename = Path.GetFileName(filepath[0]); System.Net.WebClient myWebClient = new System.Net.WebClient(); BeginInvoke(new Action(() => { myWebClient.DownloadFile(filepath[0], textBox2.Text + "\\" + filename); })); //下载进度条 string path = filepath[0]; float percent = 0; System.Net.FileWebRequest Myrq = (System.Net.FileWebRequest)System.Net.FileWebRequest.Create(path); System.Net.FileWebResponse myrp = (System.Net.FileWebResponse)Myrq.GetResponse(); long totalBytes = myrp.ContentLength; if (prog != null) { prog.Maximum = (int)totalBytes; } if (prog != null) { prog.Maximum = (int)totalBytes; } System.IO.Stream st = myrp.GetResponseStream(); System.IO.Stream so = new System.IO.FileStream(filename, System.IO.FileMode.Create); long totalDownloadedByte = 0; byte[] by = new byte[1024]; int osize = st.Read(by, 0, (int)by.Length); while (osize > 0) { totalDownloadedByte = osize + totalDownloadedByte; System.Windows.Forms.Application.DoEvents(); so.Write(by, 0, osize); if (prog != null) { prog.Value = (int)totalDownloadedByte; } osize = st.Read(by, 0, (int)by.Length); percent = (float)totalDownloadedByte / (float)totalBytes * 100; label4.Visible = true; label4.Text = "下载进度:" + percent.ToString("#0.00") + "%"; System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label4将因为循环执行太快而来不及显示信息 } so.Close(); st.Close(); } } catch (Exception ex) { MessageBox.Show(ex.Message.ToString(), "警告"); } this.Close(); MessageBox.Show("下载成功,请安装", "提示"); }