6 Replies - 947 Views - Last Post: 30 September 2017 - 06:21 PM Rate Topic: -----

#1 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

How to put all files in an array from a directory in c#?

Posted 30 September 2017 - 03:28 AM

Hi, I want to get all *.xml files from a location where there are many folders and most of them have sub-folders and sub-folders have sub-folders. In some of the folders in that location there is a folder called xml and it contains a *.xml file. I want only those *.xml files in an array which fall inside the xml folder. How do I do it?
Say the path I given to the program is "D:\MyJobs" and inside this folder there are folders and files like
1246-33
320-236
102-123
star.xml


and some of the folders say, 320-236 have sub-folders like 320-236\320\236\xml\abc.xml and 102-123 have sub-folders like
102-123\102\123\xml\123.xml, so resulting array should contain two xml files namely- abc.xml and 123.xml and not star.xml as it is
not inside a folder name xml. I've tried the below code but it has a problem. How do I correct this?

string myDirectory = textBox1.Text;
string[] xmlDir = Directory.GetDirectories(targetDirectory1 + @"\", "xml*", SearchOption.AllDirectories);
string[] xmlFilesArray1 = Directory.GetFiles(xmlDir, "*.xml", SearchOption.AllDirectories);


Is This A Good Question/Topic? 0
  • +

Replies To: How to put all files in an array from a directory in c#?

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,373
  • Joined: 02-June 10

Re: How to put all files in an array from a directory in c#?

Posted 30 September 2017 - 03:53 AM

https://www.google.c...=utf-8&oe=utf-8
Was This Post Helpful? 0
  • +
  • -

#3 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,679
  • Joined: 01-February 13

Re: How to put all files in an array from a directory in c#?

Posted 30 September 2017 - 06:24 AM

From your description, i believe you have the first step right. Get an array of the paths to all the directories/folders which have a name of "Xml*". However, then you need to iterate through each of those folder paths and get the files from each of them, searching only the top directory.

For example, after getting the xml directories, use a For Each loop to iterate through the xml directories and get the xml files from them...
	foreach (string folder in xmlDir{
		string[] xmlFiles = IO.Directory.GetFiles(folder, "*.xml");
		ListBox1.Items.AddRange(xmlFiles);
	}



In the example i added all the xml files to the ListBox using the AddRange method since 'xmlFiles' is an Array of Strings. However, you may need/want to use another (inner) For Each loop to iterate through the 'xmlFiles' and add or list them as needed for your situation.
Was This Post Helpful? 1
  • +
  • -

#4 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to put all files in an array from a directory in c#?

Posted 30 September 2017 - 07:27 AM

View PostIronRazer, on 30 September 2017 - 06:24 AM, said:

From your description, i believe you have the first step right. Get an array of the paths to all the directories/folders which have a name of "Xml*". However, then you need to iterate through each of those folder paths and get the files from each of them, searching only the top directory.

For example, after getting the xml directories, use a For Each loop to iterate through the xml directories and get the xml files from them...
	foreach (string folder in xmlDir{
		string[] xmlFiles = IO.Directory.GetFiles(folder, "*.xml");
		ListBox1.Items.AddRange(xmlFiles);
	}



In the example i added all the xml files to the ListBox using the AddRange method since 'xmlFiles' is an Array of Strings. However, you may need/want to use another (inner) For Each loop to iterate through the 'xmlFiles' and add or list them as needed for your situation.

Thanks @IronRazer for your reply. But I need the array xmlFiles to be available outside the foreach loop...how do I do that?
Was This Post Helpful? 0
  • +
  • -

#5 modi123_1  Icon User is online

  • Suitor #2
  • member icon



Reputation: 13489
  • View blog
  • Posts: 53,885
  • Joined: 12-June 08

Re: How to put all files in an array from a directory in c#?

Posted 30 September 2017 - 09:53 AM

Perhaps declare the array outside the forloop then.
Was This Post Helpful? 0
  • +
  • -

#6 IronRazer  Icon User is offline

  • Custom Control Freak
  • member icon

Reputation: 1444
  • View blog
  • Posts: 3,679
  • Joined: 01-February 13

Re: How to put all files in an array from a directory in c#?

Posted 30 September 2017 - 09:59 AM

It would probably be better to declare a List<T> at the scope you need, locally or class scoped. Then add the array of xml filenames to that List. You can use the AddRange method of the List instead of AddRange method of the ListBox Items.

For example... here i declare a new list<string> locally inside a sub or function before the loop is entered and add the xml files to it inside the loop. After the For Each loop is exited, you will have all the xml filenames in the List. The List will only exist inside the sub or function until the end of the sub or function is reached.
	List<string> xmlFileList = new List<string>();

	foreach (string folder in xmlFolders) {
		string[] xmlFiles = IO.Directory.GetFiles(folder, "*.xml");
		xmlFileList.AddRange(xmlFiles);
	}

	ListBox1.Items.AddRange(xmlFileList.ToArray);



If you need to access the List throughout the subs and/or functions in the Class, then you will need to declare the List at Class level, outside of any subs or functions that are in your Class.

C# Variable Scopes
Variable and Method Scope in Microsoft .NET
Was This Post Helpful? 1
  • +
  • -

#7 Whateva_  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 43
  • Joined: 28-August 16

Re: How to put all files in an array from a directory in c#?

Posted 30 September 2017 - 06:21 PM

Thanks IronRazor :bigsmile:/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1