//下载版本
/// <summary> /// 下载版本 /// </summary> /// <param name="FilePath">服务器文件地址完整</param> /// <param name="downLoadPath">下载到本地地址</param> /// <returns></returns> private bool temDowm(string FilePath ,string downLoadPath) { //获取文件名 string FilePath = FilePath.Substring(FilePath.LastIndexOf("/") + 1); //判断地址是否存在,不存在创建文件 if (!Directory.Exists(downLoadPath)) { Directory.CreateDirectory(downLoadPath); } //对文件地址发出请求 WebRequest request = WebRequest.Create(FilePath); WebResponse fs = null; try { fs = request.GetResponse(); } catch (Exception ex) { MessageBox.Show(ex.Message + "/r/n请确保文件名不存在特殊字符"); return false; } long contentLength = fs.ContentLength; Stream st = fs.GetResponseStream(); try { byte[] byteLength = new byte[contentLength]; int allByte = (int)contentLength; int startByte = 0; while (contentLength > 0) { int downByte = st.Read(byteLength, startByte, allByte); if (downByte == 0) { break; } startByte += downByte; allByte -= downByte; } // 打开文件写入文件 FileStream stream = new FileStream(downLoadPath + @"\" + fileName, FileMode.OpenOrCreate, FileAccess.Write); stream.Write(byteLength, 0, byteLength.Length); stream.Close(); fs.Close(); return true; } catch (Exception ex) { MessageBox.Show(ex.Message); return false; } }
标签:byteLength,文件,fs,FilePath,int,更新,ex,服务器,downByte From: https://www.cnblogs.com/MaoZhuaShi/p/18635433