Web ディレクトリ (IIS 上) からイメージ ファイルをダウンロードしようとしているので、Web クライアントを使用してファイルをループしてダウンロードしようとしました。2 つのアプリケーションを取得しました。1 つはデプロイした Web アプリで、もう 1 つは Web アプリからファイルをダウンロードしようとする WPF アプリです。ここに私のコードがあります:
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 = 新しい DirectoryInfo(フルパス);
エラー : 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);
}
デプロイした Web は C:/inetpub/wwwroot にあります。だから私は間違ったことをしていますか?複数のファイルをディレクトリにループしてダウンロードしようとしています。