Question

I have to check each xml file inside an array of different directories.

My code(still have errors):

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

I guess that you cannot store an array in a string list. Is the any other way how I can read the files in each directories stored in an array?

Was it helpful?

Solution 3

Fixed it! Just have to add and replace some codes.. :)

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

OTHER TIPS

Have you tried using AddRange?

Something like

xmlList.AddRange(getFiles); 

From what I can see, you could have also just gone with something like

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

It's not quite clear what you're trying to do, but you can use the AddRange method to add all elements of the string[] array returned by Directory.GetFiles to your list at once:

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

Also consider the following things:

  1. Your xmlList instance is not initialized, try:List<string> xmlList = new List<string>();

  2. The name of the variable file in the foreach construct is a misnomer, consider using directory instead, since that is what the "elements" of xmlLoc are.

  3. You don't really need the getFiles variable, a simple xmlList.AddRange(Directory.GetFiles(file, "*.xml")); would be sufficient in your case.

  4. Splitting on whitespace is not a good idea. Directory names (although not the examples you use), may contain whitespace themselves.

Your code looks a little to complicated. AFAICT the following would do the same:

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

Try this, you need to initiliaze the XML list, and GetFiles returns an array, so you need to call AddRange, not Add when adding to the XML list.

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);
            }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top