<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Infinite Loop - Beginner&#039;s SEO, Beginner C# &#38; JQuery Tutorials &#187; ASP.NET</title>
	<atom:link href="http://theinfiniteloopblog.com/category/asp-net/feed/" rel="self" type="application/rss+xml" />
	<link>http://theinfiniteloopblog.com</link>
	<description>Problem. Problem Solved. Loop. - The life of a programmer</description>
	<lastBuildDate>Wed, 17 Aug 2011 15:29:45 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1.3</generator>
		<item>
		<title>Recursing through a directory/filesystem using C#</title>
		<link>http://theinfiniteloopblog.com/gen/recursing-through-a-directoryfilesystem-using-c/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=recursing-through-a-directoryfilesystem-using-c</link>
		<comments>http://theinfiniteloopblog.com/gen/recursing-through-a-directoryfilesystem-using-c/#comments</comments>
		<pubDate>Wed, 02 Sep 2009 21:34:37 +0000</pubDate>
		<dc:creator>Scott</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[General]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://scottmoniz.com/programmingBlog/?p=220</guid>
		<description><![CDATA[This will teach you how to recurse through a directory/filesystem using C#.. The problem comes from not knowing how many folders/files exist. One folder can have many folders inside it, those folders can each have many folders inside it. I will show you how to recurse through the filesystem and explain it so that you [...]]]></description>
			<content:encoded><![CDATA[
<!-- ALL ADSENSE ADS DISABLED -->
<p>This will teach you how to <strong>recurse through a directory/filesystem using C#.</strong>.<br />
The problem comes from not knowing how many folders/files exist.<br />
One folder can have many folders inside it, those folders can each have many folders inside it.</p>
<p>I will show you how to recurse through the filesystem and explain it so that you UNDERSTAND it. This isnt for copy-paste.</p>
<p>We will start with a C# console application and pick a path, we'll use C: for this.<br />
First of all, we're going to need the IO namespace. The IO namespace has access to File and Directory methods.</p>
<p>At the top add these lines:</p>
<pre>
using System; //Just so we can use the shortcut Console instead of System.Console
using System.IO; //Gives us access to file, directory objects</pre>
<p>Now, in our "main" method, we are going to write the following:</p>
<pre>
string path="C:/"; //set the root path we want to recurse through.

//check if the directory exists
if(Directory.Exists(path)){
        DirectoryInfo di=new DirectoryInfo(path);
        //based on a path, this object will give you directory information about that directory
        printDirectories(di);
        //this method will actually go through our directories and print out information
}else{
        Console.WriteLine("Directory does not exist. Press Enter to exit");
        Console.ReadLine(); //when you hit return the program will terminate.
}
</pre>
<p><!--adsense--><br />
Now we will write the print directories method. It takes a DirectoryInfo object as a parameter and will use that to recurse through the file system.</p>
<p>//declared as static because were a console app, this can be private in your application if it a utility method.<br />
2 Important methods we're going to be looking at are in the DirectoryInfo object. They are</p>
<p>public FileInfo[] directoryInfoObject.GetFiles() //returns an array of FileInfo objects<br />
public DirectoryInfo[] directoryInfoObject.GetDirectories() //returns an array of DirectoryInfo objects</p>
<pre>static void printDirectories(DirectoryInfo di){
       foreach(FileInfo fi in di.GetFiles()){
              //we are going to run code for each file
              Console.WriteLine(fi.FullName); //this prints out the full file name including the path
       }

      //now that we process this directory for files, we do the same for every subdirectory in this directory
      foreach(DirectoryInfo subDir in di.GetDirectories()){
             //we are going to call the printDirectories method, with the subdirectory as the root.
            //its files will be processed, then its subdirectories will be checked
            //this happens foreach Directory in the current directory
            printDirectories(subDir); //recurse
      }
}</pre>
<p>Thats simple recursion. The most common use of this is for populating a treeview and another use that comes to mind is file searching. Keep an eye out for the next tutorial: Populating a Treeview from a file system!</p>
]]></content:encoded>
			<wfw:commentRss>http://theinfiniteloopblog.com/gen/recursing-through-a-directoryfilesystem-using-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

