ダウンロードしたい URI のリストで構成される構成ファイルがあります。例えば、
http://xyz.abc.com/Dir1/Dir3/sds.txt
http://xyz.abc.com/Dir2/Dir4/jhjs.txt
http://xyz.abc.com/Dir1/itr.txt
構成ファイルを読み取り、各 URL をコピーすると同時に、ホストと同じディレクトリ構造を作成したいと考えています。たとえば、構成ファイルの最初の行では、ローカル マシンにディレクトリ構造 Dir1/Dir3 を作成し (存在しない場合)、sds.exe を .../Dir1/Dir3/ にコピーします。
C#でこれを行うにはどうすればよいですか?
私が今まで持っているものは次のとおりです。
string myStringWebResource; //Read this from the config file
System.IO.StreamReader file =
new System.IO.StreamReader("config.txt");
// Read the config file line by line
while ((myStringWebResource = file.ReadLine()) != null)
{
// Create a new WebClient instance.
using (WebClient myWebClient = new WebClient())
{
//How do I keep the original filename, e.g. sds.txt
string outputFilename = @"" + ???";
Console.WriteLine("Downloading ...");
// Download the Web resource and save it into
// the current filesystem folder.
try
{
myWebClient.DownloadFile(myStringWebResource, outputFilename);
Console.WriteLine("Successful");
}
catch (WebException e)
{
Console.WriteLine("Fail" + e.Message);
}
}
}
これにより、構成ファイル内のすべてのファイルをダウンロードできますが、
- ダウンロード後に名前を付けなければならないという問題があります。元の名前を保持するにはどうすればよいですか?
- すべてのファイルが同じフォルダーにダウンロードされます。ホストからフォルダ構造を複製するにはどうすればよいですか?