2013-04-17 42 views
1

我有一个配置文件,其中包含我想要下载的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); 
     } 
    } 
} 

虽然这让我下载配置文件中的所有文件,

  1. 我有下载后他们的名字,怎么这个问题我可以保留原来的名字吗?
  2. 所有文件都下载到同一个文件夹中。我怎么能从主机复制文件夹结构?

回答

0

我的建议是我们要过滤掉主机。然后将每个剩余的字符串分割到/上。然后构建一个将字符串[]作为参数的方法来创建目录结构。使用原始路径(无主机)将该文件的文件名(数组中的最后一项)移动到该目录中。

如果主机是可变的,请删除第三个/之前的所有内容。

+0

那很好。但你能告诉我如何过滤出主机 –

相关问题