一个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加共享文件夹目录?
或者是有没有其他的什么比较好的方法?最好详细一点啊,我以前没写过类似的
谢谢大家!

解决方案 »

  1.   

    你的程序是怎么和服务器通信的呢?是Remoting?还是WebService,还是其它的方式?
      

  2.   

    DownloadFile 方法将来自 address 参数指定的 URI 的数据下载到本地文件。此方法在下载资源时阻止。若要下载资源并在等待服务器响应的同时继续执行,请使用 DownloadFileAsync 方法之一。如果 BaseAddress 属性不是空字符串 (""),且 address 不包含绝对 URI,则 address 必须是相对 URI,此 URI 与 BaseAddress 组合在一起构成所请求数据的绝对 URI。如果 QueryString 属性不是空字符串,则将它追加到 address。此方法使用 RETR 命令下载 FTP 资源。对于 HTTP 资源,使用 GET 方法。
    下面的代码示例将文件从 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);
      

  3.   

    你的方式也可以,如果是仅仅下载文件可以直接用WebClient类来做。
      

  4.   

        System.Net.WebClient _Client = new System.Net.WebClient();            _Client.DownloadFile("Http://asdfasdf/asdf.zip", 保存到本地文件);
      

  5.   

    Remoting和WebService哪种方法方便一点?
      

  6.   

    如果你不想共享你的服务器资源,我觉得你使用Remoting或是WebService等哪一个也是可以的。可以在服务器端读取指定的文件转成Byte数组传到客户端,然后再把文件还原。