|
楼主 |
发表于 2013-7-13 13:57:21
|
显示全部楼层
我程序主要代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Runtime.InteropServices;
using System.Net.NetworkInformation;
using System.Net;
using System.Threading;
namespace AutoUpdateSetupClient
{
public partial class UpdateMainForm : Form
{
public UpdateMainForm()
{
InitializeComponent();
}
/// <summary>
/// c#,.net 下载文件
/// </summary>
/// <param name="url">下载文件地址</param>
///
/// <param name="filename">下载后的文件名</param>
/// <param name="Prog">用于显示的进度条</param>
///
public void DownloadFile(string url, string filename, System.Windows.Forms.ProgressBar prog, System.Windows.Forms.Label label1)
{
long lStartPos = 0;
System.IO.FileStream fileModeCreate;
try
{
float percent = 0;
System.Net.HttpWebRequest myrq1 = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
System.Net.HttpWebResponse myrp1 = (System.Net.HttpWebResponse)myrq1.GetResponse();
long totalBytes1 = myrp1.ContentLength; //总文件大小
if (System.IO.File.Exists(filename))
{
fileModeCreate = System.IO.File.OpenWrite(filename);
lStartPos = fileModeCreate.Length;
if (lStartPos < totalBytes1)
{
fileModeCreate.Seek(lStartPos, System.IO.SeekOrigin.Current); //移动文件流中的当前指针
}
else
{
fileModeCreate.Close();
fileModeCreate = new System.IO.FileStream(filename, System.IO.FileMode.Create);
lStartPos = 0;
}
}
else
{
fileModeCreate = new System.IO.FileStream(filename, System.IO.FileMode.Create);
lStartPos = 0;
}
System.Net.HttpWebRequest myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
if (lStartPos > 0 && lStartPos < totalBytes1)
myrq.AddRange((int)lStartPos); //设置Range值
System.Net.HttpWebResponse myrp = (System.Net.HttpWebResponse)myrq.GetResponse();
long totalBytes = lStartPos + myrp.ContentLength; //已下载的文件大小+请求到的剩余文件大小
if (prog != null)
{
prog.Maximum = (int)totalBytes;
}
System.IO.Stream getResponseStream = myrp.GetResponseStream();
long totalDownloadedByte = lStartPos;
byte[] fileByte = new byte[1024];
int osize = getResponseStream.Read(fileByte, 0, (int)fileByte.Length);
try
{
while (osize > 0)
{
totalDownloadedByte = osize + totalDownloadedByte;
System.Windows.Forms.Application.DoEvents();
fileModeCreate.Write(fileByte, 0, osize);
if (prog != null)
{
prog.Value = (int)totalDownloadedByte;
}
osize = getResponseStream.Read(fileByte, 0, (int)fileByte.Length);
percent = (float)totalDownloadedByte / (float)totalBytes * 100;
label1.Text = "当前补丁下载进度" + percent.ToString() + "%";
System.Windows.Forms.Application.DoEvents(); //必须加注这句代码,否则label1将因为循环执行太快而来不及显示信息
}
}
catch (System.Exception ex)
{
label错误信息.Text = "下载过程中出现错误,请重新启动程序:" + ex.ToString();
fileModeCreate.Close();
getResponseStream.Close();
DownloadFile(url, filename, progressBar进度条, label进度条显示);
}
fileModeCreate.Close();
getResponseStream.Close();
Application.Exit(); //退出
}
catch (System.Exception ex)
{
label错误信息.Text = "网络连接错误,请重新启动程序:" + ex.ToString();
DownloadFile(url, filename, progressBar进度条, label进度条显示);
}
}
private void UpdateMainForm_Shown(object sender, EventArgs e)
{
StreamReader reader = new StreamReader(System.Windows.Forms.Application.StartupPath + "\\UpdateFileList.txt");
string url = reader.ReadLine();
reader = new StreamReader(System.Windows.Forms.Application.StartupPath + "\\SetupExe.txt");
string afterFilename = System.Windows.Forms.Application.StartupPath + "\\" + reader.ReadLine();
System.Windows.Forms.Application.DoEvents(); //
DownloadFile(url, afterFilename, progressBar进度条, label进度条显示);
}
private void UpdateMainForm_FormClosed(object sender, FormClosedEventArgs e)
{
System.Environment.Exit(System.Environment.ExitCode);
this.Dispose();
this.Close();
}
}
}
|
|