C# 文件下载(WebClient)
private void DownLoadFile()
{
try
{
WebClient client = new WebClient();
if (!Directory.Exists(Application.StartupPath + @""update"))
{
Directory.CreateDirectory(Application.StartupPath + @""update");
}
for (int i = 0; i <= this.listView1.Items.Count - 1; i++)
{
string fileName = this.listView1.Items[i].SubItems[0].Text;
string url = @"http://172.16.120.155/update/UpdateFile/" + fileName;
string file = Application.StartupPath + """update""" + fileName;
this.label2.Text = "正在下载:" + fileName;
Stream myStream = client.OpenRead(url);
byte[] bBuffer = new byte[10240];
int nRealCount = 0;
DateTime dtStart = DateTime.Now;
TimeSpan ts;
int lenth = Convert.ToInt32(this.listView1.Items[i].SubItems[1].Text.ToString());
int cur = 0;
progressBar1.Value = 0;
FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
//do
//{
// dtStart = DateTime.Now;
// nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
// if(nRealCount > 0)
// {
// fs.Write(bBuffer, 0, nRealCount);
// }
// ts = DateTime.Now - dtStart;
// cur = cur + nRealCount;
// double pe = cur;
// pe = pe * 100 / lenth;
// int va = Convert.ToInt32(pe);
// progressBar1.Value = va;
// this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB";
// MessageBox.Show("" + ts.Milliseconds + " " + ts.TotalMilliseconds);
// // this.label2.Text = "正在下载:" + fileName + " " + cur /1024 + " KB/" + lenth / 1024 + " KB" + " 下载速度:"+((double )nRealCount/(int)(ts.TotalMilliseconds/1000))+" kb/s";
// // 计算流量可以用nRealCount / ts.Seconds来进行估算
//}
int value = 0;
int counter = 1;
do
{
if (counter % 10 == 0)
{
ts = DateTime.Now - dtStart;
int perSecondsValue = (int)((double)value / (ts.TotalMilliseconds / 1000)) / 1024;
this.label3.Text = "速度:" + perSecondsValue + " k/s";
dtStart = DateTime.Now;
value = 0;
}
nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
if (nRealCount > 0)
{
fs.Write(bBuffer, 0, nRealCount);
}
cur = cur + nRealCount;
double pe = cur;
pe = pe * 100 / lenth;
int va = Convert.ToInt32(pe);
progressBar1.Value = va;
this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB";
value += nRealCount;
counter++;
}
while (nRealCount > 0);
fs.Close();
// client.DownloadFile(url, file);
this.listView1.Items[i].SubItems.Add("已下载");
}
this.progressBar1.Visible = false;
this.label3.Visible = false;
this.label2.Text = "正在更新文件,请稍后...";
string[] fileDir = Directory.GetFileSystemEntries(Application.StartupPath + @""update");
foreach (string str in fileDir)
{
int pos = str.LastIndexOf(@""");
string FileName = str.Substring(pos);
string FilePath = Application.StartupPath + FileName;
File.Copy(str, FilePath, true);
}
this.label2.Text = "程序更新已完成!";
this.label1.Text = this.finish;
this.btnNext.Enabled = false;
this.listView1.Visible = false;
this.btnExit.Text = "完成(&F)";
step++;
}
catch (Exception errMsg)
{
MessageBox.Show(errMsg.Message);
}
}
{
try
{
WebClient client = new WebClient();
if (!Directory.Exists(Application.StartupPath + @""update"))
{
Directory.CreateDirectory(Application.StartupPath + @""update");
}
for (int i = 0; i <= this.listView1.Items.Count - 1; i++)
{
string fileName = this.listView1.Items[i].SubItems[0].Text;
string url = @"http://172.16.120.155/update/UpdateFile/" + fileName;
string file = Application.StartupPath + """update""" + fileName;
this.label2.Text = "正在下载:" + fileName;
Stream myStream = client.OpenRead(url);
byte[] bBuffer = new byte[10240];
int nRealCount = 0;
DateTime dtStart = DateTime.Now;
TimeSpan ts;
int lenth = Convert.ToInt32(this.listView1.Items[i].SubItems[1].Text.ToString());
int cur = 0;
progressBar1.Value = 0;
FileStream fs = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);
//do
//{
// dtStart = DateTime.Now;
// nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
// if(nRealCount > 0)
// {
// fs.Write(bBuffer, 0, nRealCount);
// }
// ts = DateTime.Now - dtStart;
// cur = cur + nRealCount;
// double pe = cur;
// pe = pe * 100 / lenth;
// int va = Convert.ToInt32(pe);
// progressBar1.Value = va;
// this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB";
// MessageBox.Show("" + ts.Milliseconds + " " + ts.TotalMilliseconds);
// // this.label2.Text = "正在下载:" + fileName + " " + cur /1024 + " KB/" + lenth / 1024 + " KB" + " 下载速度:"+((double )nRealCount/(int)(ts.TotalMilliseconds/1000))+" kb/s";
// // 计算流量可以用nRealCount / ts.Seconds来进行估算
//}
int value = 0;
int counter = 1;
do
{
if (counter % 10 == 0)
{
ts = DateTime.Now - dtStart;
int perSecondsValue = (int)((double)value / (ts.TotalMilliseconds / 1000)) / 1024;
this.label3.Text = "速度:" + perSecondsValue + " k/s";
dtStart = DateTime.Now;
value = 0;
}
nRealCount = myStream.Read(bBuffer, 0, bBuffer.Length);
if (nRealCount > 0)
{
fs.Write(bBuffer, 0, nRealCount);
}
cur = cur + nRealCount;
double pe = cur;
pe = pe * 100 / lenth;
int va = Convert.ToInt32(pe);
progressBar1.Value = va;
this.label2.Text = "正在下载:" + fileName + " " + cur / 1024 + " KB/" + lenth / 1024 + " KB";
value += nRealCount;
counter++;
}
while (nRealCount > 0);
fs.Close();
// client.DownloadFile(url, file);
this.listView1.Items[i].SubItems.Add("已下载");
}
this.progressBar1.Visible = false;
this.label3.Visible = false;
this.label2.Text = "正在更新文件,请稍后...";
string[] fileDir = Directory.GetFileSystemEntries(Application.StartupPath + @""update");
foreach (string str in fileDir)
{
int pos = str.LastIndexOf(@""");
string FileName = str.Substring(pos);
string FilePath = Application.StartupPath + FileName;
File.Copy(str, FilePath, true);
}
this.label2.Text = "程序更新已完成!";
this.label1.Text = this.finish;
this.btnNext.Enabled = false;
this.listView1.Visible = false;
this.btnExit.Text = "完成(&F)";
step++;
}
catch (Exception errMsg)
{
MessageBox.Show(errMsg.Message);
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· 周边上新:园子的第一款马克杯温暖上架
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
· 使用C#创建一个MCP客户端