一个WinForm程序,需要从服务器上下载指定的多个文件。
我在网上也找了一些方法,比如:public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
System.Net.HttpWebResponse myrp = (System.Net.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 System.IO.FileStream(filename, System.IO.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;
System.Windows.Forms.Application.DoEvents();
so.Write(by, 0, osize);
if (prog != null)
{
prog.Value = (int)totalDownloadedByte;
}
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
try
{ }
catch (System.Exception)
{
throw;
}
}URL:为下载文件地址
filename:为下载文件名
但是我不知道这个服务器地址怎么写?IP加共享文件夹目录?
或者是有没有其他的什么比较好的方法?最好详细一点啊,我以前没写过类似的
谢谢大家!
我在网上也找了一些方法,比如:public void DownloadFile(string URL, string filename, System.Windows.Forms.ProgressBar prog)
{
System.Net.HttpWebRequest Myrq = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(URL);
System.Net.HttpWebResponse myrp = (System.Net.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 System.IO.FileStream(filename, System.IO.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;
System.Windows.Forms.Application.DoEvents();
so.Write(by, 0, osize);
if (prog != null)
{
prog.Value = (int)totalDownloadedByte;
}
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
try
{ }
catch (System.Exception)
{
throw;
}
}URL:为下载文件地址
filename:为下载文件名
但是我不知道这个服务器地址怎么写?IP加共享文件夹目录?
或者是有没有其他的什么比较好的方法?最好详细一点啊,我以前没写过类似的
谢谢大家!
解决方案 »
- 如何在C#程序启动时在桌面右下角出现一个窗口
- c#如何得到两个日期之间的月间隔
- 想考高级软件设计师,有意义吗??
- 菜鸟问题(怎样在循环中输入出错时停止.我刚刚学习C#不要回答得太难.)
- dropdownlist联动问题
- DataGridView1为什么不能显示
- 怎么把Image对象转成流或转成数组,我想把它通过udp发送出去.
- 急急!!打开IE自动进入http://4-counter.com/,而且自动增加收藏夹链接位置,修改注册表无效,将注册表恢复还是没有用
- 求助:求一正则表达式
- C#类在windows 2008 R2 64bit无法识别
- 请教关于事务的问题:TransactionScop与数据库的事务的性能的差距
- 100分求 我写的递归程序问题.不够再加(C#)
下面的代码示例将文件从 http://www.contoso.com 下载到本地硬盘。 string remoteUri = "http://www.contoso.com/library/homepage/images/";
string fileName = "ms-banner.gif", myStringWebResource = null;
// 创建WebClient实例.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource,fileName);
Console.WriteLine("Successfully Downloaded File \"{0}\" from \"{1}\"", fileName, myStringWebResource);
Console.WriteLine("\nDownloaded file saved in the following file system folder:\n\t" + Application.StartupPath);