在web目录中循环多个文件并下载

时间:2013-09-11 01:18:04

标签: c# asp.net directory webserver webclient

我正在尝试从我的网络目录(在IIS上)下载图像文件,因此我尝试使用Web客户端通过循环来下载文件。我有2个应用程序,1个是我部署的Web应用程序,另一个是WPF应用程序,我尝试从Web应用程序下载文件。这是我的代码:

     public StartWindow()
        {
            InitializeComponent();        
            string fileUploadDirectory = ConfigurationManager.AppSettings.Get("path"); 
          //  string fullpath = System.IO.Path.Combine(fileUploadDirectory, daoWordPuzzle.GetfileURL().Substring(2)); // removes  ( ~/ ) . 
 string fullpath = "http://localhost/iStellarMobile_deploy/Designs/";
                DirectoryInfo d = new DirectoryInfo(fullpath);
                FileInfo[] Files = d.GetFiles("*.png");
                foreach (FileInfo file in Files)
                {
                    string root = file.DirectoryName;
                    DownloadFile(root);
                }
        }

   protected void DownloadFile(string urlAddress)
    {
        client = new WebClient();     
        string currentpath = System.IO.Directory.GetCurrentDirectory() + @"\ImagesTest"; // Save under ImagesTest folder.
        client.DownloadFile(urlAddress, currentpath);    
    }

图片位于Designs的文件夹中..这就是我想下载的内容...... 然后在我的App.config中:

  <appSettings>
<add key="path" value="http://localhost/iStellarMobile_deploy" />

但是当我运行WPF应用程序时,它在这一行显示错误:

  

DirectoryInfo d = new DirectoryInfo(fullpath);

错误:不支持URI格式。

之前我正在使用(这适用于下载文本文件,但不适用于多个文本文件):

protected void DownloadData(string strFileUrlToDownload)
    {
        WebClient client = new WebClient();
        byte[] myDataBuffer = client.DownloadData(strFileUrlToDownload);         

        MemoryStream storeStream = new MemoryStream();

        storeStream.SetLength(myDataBuffer.Length);
        storeStream.Write(myDataBuffer, 0 , (int)storeStream.Length);

        storeStream.Flush();

        string currentpath = System.IO.Directory.GetCurrentDirectory() + @"\Folder"; //folder to contain files.

        using (FileStream file = new FileStream(currentpath, FileMode.Create, System.IO.FileAccess.ReadWrite))
        {
            byte[] bytes = new byte[storeStream.Length];
            storeStream.Read(bytes, 0, (int)storeStream.Length);
            file.Write(myDataBuffer, 0, (int)storeStream.Length);
            storeStream.Close();
        }

        //The below Getstring method to get data in raw format and manipulate it as per requirement
        string download = Encoding.ASCII.GetString(myDataBuffer);


    }

我部署的网站位于C:/ inetpub / wwwroot。我做错了什么?我试图通过在目录中循环它们来下载多个文件。

0 个答案:

没有答案