0

我正在尝试从我的 web 目录(在 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 上。那么我做错了吗?我正在尝试通过在目录中循环来下载多个文件。

4

0 回答 0