RT  ;一个是WebClient里的DownloadFile方法;另一个是:“
WebRequest req = WebRequest.Create(url);  
WebResponse pos = req.GetResponse();  
long totalbytes = pos.ContentLength;  
Stream s = pos.GetResponseStream(); 
FileStream fs = new FileStream(savefullpath, FileMode.OpenOrCreate, FileAccess.Write); 
”来下载文件!
不知道这两种方法有什么区别;另外“WebRequest req = WebRequest.Create(url);  ”这句我在使用时感觉没什么用来着 也请大神下作用

解决方案 »

  1.   

    DownloadFile方法当然要调用WebRequest来下载文件啦。WebRequest有很多种,不仅仅是 HttpWebRequest,也包括 FtpWebRequest、FileWebReqeust等。当你给出的路径是基于http/https协议的,那么DownloadFile也会自动调用HttpWebRequest下载。WebClient封装成更方便的形式,让你少操心一些低级的东西(除非你确实能做出比WebClient更强的代码的时候)。
      

  2.   

    我删了呀 也没影响 :   public string GetF(string path1,string path2) 
        {
            try
            {
                WebClient wc = new WebClient();
                wc.DownloadFile(@"E:\MyWeb\MyWeb2\Folder\" + path1, path2);
       
                return "下载成功";
            }
            catch 
            {
                return "下载失败";
            }直接这样程序照样运行
      

  3.   

    还有就是使用WebResponse
    获取响应的数据流Stream,然后打开本地文件将传输数据写入文件。这种方法和 DownloadFile方法有什么区别吗
      

  4.   

    刚刚重写了代码 :
     public string GetF(string path1,string path2) 
        {
            try
            {
                //WebRequest wr = WebRequest.Create(@"E:\MyWeb\MyWeb2\Folder\");
                WebClient wc = new WebClient();
                wc.DownloadFile(@"E:\MyWeb\MyWeb2\Folder\" + path1, path2);
                return "下载成功";
            }
            catch 
            {
                return "下载失败";
            }
            
        }
        [WebMethod]
        public string GetFF(string path1, string path2)
        {
            try
            {
                WebRequest wr = WebRequest.Create(@"E:\MyWeb\MyWeb2\Folder\"+path1);
                WebResponse pos = wr.GetResponse();
                Stream st = pos.GetResponseStream();
                FileStream fs = new FileStream(path2,FileMode.OpenOrCreate,FileAccess.Write);
                BinaryReader br = new BinaryReader(st);
                StreamReader reader = new StreamReader(st);
                byte[] bt = br.ReadBytes((int)st.Length);
                fs.Write(bt,0,bt.Length);
                st.Close();
                fs.Close();
                
                return "下载成功";
            }
            catch
            {
                return "下载失败";
            }    }
    这两种方法都可以  就是不知道区别