关于断点续传,值不在预期的范围内
在做一个pda在线更新
下载的时候出现异常值不在预期的范围内
下面是代码
private string urlPath = "http://xx.xx.xx.xx/iss/";//根据实际需要修改
private string Filename = "D:\\测试";
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DownFile(urlPath + "client.exe", Filename, this.progressBar1);
}
public static void DownFile(string URL, string Filename, ProgressBar Prog)
{
try
{
HttpWebRequest Myrq = (HttpWebRequest)HttpWebRequest.Create(URL);
HttpWebResponse myrp = (HttpWebResponse)Myrq.GetResponse();
long totalBytes = myrp.ContentLength;
if (Prog != null)
{
Prog.Maximum = (int)totalBytes;
}
System.IO.Stream st = myrp.GetResponseStream();
System.IO.Stream so = new FileStream(Filename, 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;
Application.DoEvents();
so.Write(by, 0, osize);
Prog.Value = (int)totalDownloadedByte;
osize = st.Read(by, 0, (int)by.Length);
System.Windows.Forms.Application.DoEvents();
}
so.Close();
st.Close();
MessageBox.Show("下载完毕!", "下载提示:", MessageBoxButtons.OK, MessageBoxIcon.Asterisk,
MessageBoxDefaultButton.Button1);
}
catch (Exception ex)
{
Console.WriteLine(ex.ToString());
MessageBox.Show("下载过程中出现错误:" + ex.ToString(), "系统提示");
}
DialogResult dr = MessageBox.Show("下载完成,是否要打开程序","提示", MessageBoxButtons.OK,
MessageBoxIcon.Asterisk,MessageBoxDefaultButton.Button1);
if (dr == DialogResult.OK)
{
System.Diagnostics.Process.Start("client.exe");
Application.Exit();
}
else
{
Console.WriteLine("放弃程序更新");
}
}
------解决方案--------------------
参数这样
System.IO.Stream so = new FileStream(Filename, FileMode.Append, FileAccess.Write);