WinForm会的进来看看

namcer8816 2012-07-24 09:01:15
我想实现 winfrom窗体中 button事件 实现,当点击时候下载制定文件路径的zip 文件。 帮帮忙 新手啊
!!
...全文
135 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hlqnlbsn627 2012-07-24
  • 打赏
  • 举报
回复
楼猪加油,我也在想这个问题
lenovore 2012-07-24
  • 打赏
  • 举报
回复

using System.Net;
using System.IO;

/// <summary>
/// 下载服务器至客户端
/// </summary>
/// <param name="URL">被下载的地址, 绝对路径</param>
/// <param name="Dir">另存放的目录</param>
public static void Download(string URL, string Dir)
{
WebClient client = new WebClient();
string fileName = URL.Substring(URL.LastIndexOf("/") + 1); //被下载的名
string Path = Dir + fileName; //另存为的绝对路径+名
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";

//request.Timeout = 300000;
request.KeepAlive = true;
request.AllowAutoRedirect = true;

request.Accept = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, */*";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; EmbeddedWB 14.52 from: http://www.bsalsa.com/ EmbeddedWB 14.52; .NET CLR 2.0.50727; CIBA)";

request.ContentType = "application/x-www-form-urlencoded";

HttpWebResponse webresponse = null;
try
{
webresponse = request.GetResponse() as HttpWebResponse;
if (webresponse != null)
{
Stream st = webresponse.GetResponseStream();
Stream so = new FileStream(Path, FileMode.Create);
byte[] by = new byte[1024];
int osize = st.Read(by, 0, (int)by.Length);
while (osize > 0)
{
so.Write(by, 0, osize);
osize = st.Read(by, 0, (int)by.Length);
}
so.Close();
st.Close();
}
}
catch (System.Exception e)
{
}
finally
{
if (webresponse != null)
{
webresponse.Close();
webresponse = null;
}
if (request != null)
{
request.Abort();
request = null;
}
}

}
sjms_xf1 2012-07-24
  • 打赏
  • 举报
回复
这个问题不难,对不起是在没时间写这个,友情帮顶了,免得你沉贴
shizhusz110 2012-07-24
  • 打赏
  • 举报
回复
一楼正确+
SocketUpEx 2012-07-24
  • 打赏
  • 举报
回复
不行就不行了

[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:
using System;
using System.Net;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebClient webClient = new WebClient();
webClient.DownloadFil……
[/Quote]
namcer8816 2012-07-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
using System;
using System.Net;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebClient webClient = new WebClient();
webClient.DownloadFile(u……
[/Quote]
不行啊!
SocketUpEx 2012-07-24
  • 打赏
  • 举报
回复
using System;
using System.Net;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebClient webClient = new WebClient();
webClient.DownloadFile(url, path);

Console.ReadKey();
}
}
}

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧