【问题标题】:Read each file inside different directories stored in an array读取存储在数组中的不同目录中的每个文件
【发布时间】:2012-09-04 04:27:25
【问题描述】:

我必须检查不同目录数组中的每个 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);
}

我猜您不能将数组存储在字符串列表中。还有其他方法可以读取存储在数组中的每个目录中的文件吗?

【问题讨论】:

    标签: c# arrays getdirectories


    【解决方案1】:

    您是否尝试过使用AddRange

    类似

    xmlList.AddRange(getFiles); 
    

    据我所知,你也可以选择类似的东西

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

    【讨论】:

      【解决方案2】:

      不太清楚您要做什么,但您可以使用AddRange 方法将Directory.GetFiles 返回的string[] 数组的所有元素一次添加到您的列表中:

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

      还要考虑以下几点:

      1. 你的xmlList实例没有初始化,试试:List&lt;string&gt; xmlList = new List&lt;string&gt;();

      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"));
      }
      

      【讨论】:

        【解决方案3】:

        修好了!只需要添加和替换一些代码.. :)

        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);
             }
        }
        

        【讨论】:

          【解决方案4】:

          试试这个,需要初始化XML列表,GetFiles返回一个数组,所以添加到XML列表时需要调用AddRange,而不是Add。

          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);
                      }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2015-05-28
            • 1970-01-01
            • 1970-01-01
            • 2012-10-25
            • 1970-01-01
            • 1970-01-01
            • 2017-03-17
            相关资源
            最近更新 更多