读取存储在数组中的不同目录中的每个文件

问题描述:

我必须检查不同目录数组内的每个xml文件。读取存储在数组中的不同目录中的每个文件

我的代码(仍然有错误):

string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening"; 
//Get each path and remove whitespaces 
string[] paths = files.Split(new[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries); 
//Use xmlLoc for adding \ to each file 
List<string> xmlLoc = new List<string>(); 
//get the files in directories 
string[] getFiles; 
//contains the files of each directory 
List<string> xmlList 

//Add \ each paths variable and store it in xmlLoc list 
foreach (string s in paths) 
{ 
    xmlLoc.Add(s + @"\"); 
} 

//get the xml files of each directory in xmlLoc and store it in xmlList 
foreach (string file in xmlLoc) 
{ 
    getFiles = Directory.GetFiles(file, "*.xml"); 
    //the code below lists an error "cannot convert from string[] to string" 
    xmlList.Add(getFiles); 
} 

我猜你不能存储在一个字符串列表的数组。是否有其他方式可以读取存储在数组中的每个目录中的文件?

定了!只需要添加和替换部分代码.. :)

string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening"; 
//Get each path and remove whitespaces 
string[] paths = files.Split(new[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries); 
//Use xmlLoc for adding \ to each file 
List<string> xmlLoc = new List<string>(); 
//get the files in directories 
string[] getFiles; 

//Add \ each paths variable and store it in xmlLoc list 
foreach (string s in paths) 
{ 
    xmlLoc.Add(s + @"\"); 
} 

//get the xml files of each directory in xmlLoc and loop it to read the files 
foreach (string directory in xmlLoc) 
{ 
    getFiles = Directory.GetFiles(directory, "*.xml"); 
    foreach(string files in getFiles) 
    { 
     MessageBox.Show(files); 
    } 
} 

您是否尝试过使用AddRange

喜欢的东西

xmlList.AddRange(getFiles); 

从我所看到的,你可能也刚刚经历的东西,如

List<string> xmlList = files.Split(new[] {';', ' '}, StringSplitOptions.RemoveEmptyEntries). 
    SelectMany(p => Directory.GetFiles(p, "*.xml")). 
    ToList(); 

这不是很清楚你想要做什么,但你可以使用AddRange方法将Directory.GetFiles返回的string[]阵列的所有元素一次性添加到列表中:

string[] getFiles = Directory.GetFiles(file, "*.xml"); 
xmlList.AddRange(getFiles); 

还要考虑以下几点:

  1. xmlList实例未初始化,请尝试:List<string> xmlList = new List<string>();

  2. foreach结构变量file的名称是用词不当,可以考虑使用directory代替,因为这是对的xmlLoc“元素”是。

  3. 你真的不需要getFiles变量,简单的xmlList.AddRange(Directory.GetFiles(file, "*.xml"));就足够你的情况。

  4. 分割空白不是一个好主意。目录名称(尽管不是你使用的例子),可能自己包含空白。

您的代码看起来有点复杂。 AFAICT下面会做同样的:

string directories = /* ... whatever ... */; 
List<string> xmlList = new List<string>(); 

foreach (string directory in string.Split(new[] {';'}, StringSplitOptions..RemoveEmptyEntries)) 
{ 
    string dir = directory.Trim(); 

    if (!dir.EndsWith(Path.DirectorySeparator)) 
    dir += Path.DirectorySeparator; 

    xmlList.AddRange(Directory.GetFiles(dir, "*.xml")); 
} 

试试这个,你需要initiliaze的XML列表,并GetFiles的返回数组,所以你需要调用的AddRange,增加了XML时未添加名单。

string files = "C:\Hello; C:\Hi; D:\Goodmorning; D:\Goodafternoon; E:\Goodevening"; 
      //Get each path and remove whitespaces 
      string[] paths = files.Split(new[] { ';', ' ' }, StringSplitOptions.RemoveEmptyEntries); 
      //Use xmlLoc for adding \ to each file 
      List<string> xmlLoc = new List<string>(); 
      //get the files in directories 
      string[] getFiles; 
      //contains the files of each directory 
      List<string> xmlList = new List<string>(); 

      //Add \ each paths variable and store it in xmlLoc list 
      foreach (string s in paths) 
      { 
       xmlLoc.Add(s + @"\"); 
      } 

      //get the xml files of each directory in xmlLoc and store it in xmlList 
      foreach (string file in xmlLoc) 
      { 
       getFiles = Directory.GetFiles(file, "*.xml"); 
       //the code below lists an error "cannot convert from string[] to string" 
       xmlList.AddRange(getFiles); 
      }