文件的下载:
try{
string savePath = ""//保存路劲
downFileUrl = "";//下载文件链接地址
WebClient wcClient = new WebClient();
WebRequest webReq = WebRequest.Create(downFileUrl);
WebResponse webRes = webReq.GetResponse();
int fileLength = webRes.ContentLength;
Stream srm = webRes.GetResponseStream();
StreamReader srmReader = new StreamReader(srm);
byte[] bufferbyte = new byte[fileLength];
int allByte = (int)bufferbyte.Length;
int startByte = 0;
while (fileLength > 0)
{
//Application.DoEvents();
int downByte = srm.Read(bufferbyte, startByte, allByte);
if (downByte == 0) { break; };
startByte += downByte;
allByte -= downByte;
}
if (!File.Exists(savePath))
{
string[] dirArray = savePath.Split('\\');
string temp = string.Empty;
for (int i = 0; i < dirArray.Length - 1; i++)
{
temp += dirArray[i].Trim() + "\\";
if (!Directory.Exists(temp))Directory.CreateDirectory(temp);
}
}
FileStream fs = new FileStream(savePath, FileMode.OpenOrCreate, FileAccess.Write);
fs.Write(bufferbyte, 0, bufferbyte.Length);
srm.Close();
srmReader.Close();
fs.Close();
}
catch (WebException ex)
{
throw ex;
}