c#中用Webclient类能否遍历文件夹下的所有文件,依次读取呢??
例如             WebClient client1 = new WebClient();            byte[] buffer1 = client1.DownloadData("***");
能否通过某些语句依次遍历文件夹下的文件,依次downloaddata呢??

解决方案 »

  1.   

    public static bool DownloadFile(string URL,string FilePath)
    {
         webClicent client1=new WebClient();
         Stream WebStream;
         try
         {
            webStream=client1.OpenRead(URL);
         }
         catch(Exception er1)
         {
            return false;
         }
         StreamReader reader=new StreamReader(WebStream);
         string data=reader.ReadTOEnd();
         try
         {
            FileStream localStream=new FileStream(FilePath,FileMode.Create,FileAccess.Write);
            StreamWriter writer=new StreamWriter(localStream);
            writer.Write(data);
            writer.Close();
            localStream.Close();
            return true;
         }
          catch(Exception)
          {
             return false;
          }
    }
      

  2.   

    不行,因为Http没有文件夹的概念。
    当然,有些服务器的确会返回某些路径下的文件列表,但是对于客户端来说,它也是以HTML页面的形式出现的。
      

  3.   

    参考下面这个,这个就是我遍历文件夹下所有文件,进行下载的(我自动升级时用的)private void DownUpdateFile()
    {
                try
                {
                    mainAppExe = updaterXmlFiles.GetNodeValue("//EntryPoint");
                    Process[] allProcess = Process.GetProcesses();
                    foreach (Process p in allProcess)
                    {                    if (p.ProcessName.ToLower() + ".exe" == mainAppExe.ToLower())
                        {
                            for (int i = 0; i < p.Threads.Count; i++)
                                p.Threads[i].Dispose();
                            p.Kill();
                            isRun = true;
                        }
                    }
                    //上面那些可以不用管,从下面开始看读取文件的                WebClient wcClient = new WebClient();
                    for (int i = 0; i < this.lvUpdateList.Items.Count; i++)
                    {
                        string UpdateFile = lvUpdateList.Items[i].Text.Trim();
                        string updateFileUrl = updateUrl + "/" + lvUpdateList.Items[i].Text.Trim();
                        long fileLength = 0;                    WebRequest webReq = WebRequest.Create(updateFileUrl);
                        WebResponse webRes = webReq.GetResponse();
                        fileLength = webRes.ContentLength;                    lbState.Text = "正在下载更新文件,请稍后...";
                        pbDownFile.Value = 0;
                        pbDownFile.Maximum = (int)fileLength;                    try
                        {
                            Stream srm = webRes.GetResponseStream();
                            StreamReader srmReader = new StreamReader(srm);
                            byte[] bufferbyte = new byte[fileLength];
                            int allByte = (int)bufferbyte.Length;
                            int startByte = 0;
                            while (fileLength > 0)
                            {
                                Application.DoEvents();
                                int downByte = srm.Read(bufferbyte, startByte, allByte);
                                if (downByte == 0) { break; };
                                startByte += downByte;
                                allByte -= downByte;
                                pbDownFile.Value += downByte;                            float part = (float)startByte / 1024;
                                float total = (float)bufferbyte.Length / 1024;
                                int percent = Convert.ToInt32((part / total) * 100);                            this.lvUpdateList.Items[i].SubItems[2].Text = percent.ToString() + "%";                        }                        string tempPath = tempUpdatePath + UpdateFile;
                            CreateDirtory(tempPath);
                            FileStream fs = new FileStream(tempPath, FileMode.OpenOrCreate, FileAccess.Write);
                            fs.Write(bufferbyte, 0, bufferbyte.Length);
                            srm.Close();
                            srmReader.Close();
                            fs.Close();
                        }
                        catch (WebException ex)
                        {
                            MessageBox.Show("更新文件下载失败!" + ex.Message.ToString(), "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                    InvalidateControl();
                    this.Cursor = Cursors.Default;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
    }