<?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>Weng Kien&#039;s Blog &#187; C#</title>
	<atom:link href="http://www.wengkien.com/category/c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.wengkien.com</link>
	<description>...Viva La Vida...</description>
	<lastBuildDate>Tue, 11 Oct 2011 09:30:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Using Stack to retrieve all the JPG files</title>
		<link>http://www.wengkien.com/2009/05/15/using-stack-to-retrieve-all-the-jpg-files/</link>
		<comments>http://www.wengkien.com/2009/05/15/using-stack-to-retrieve-all-the-jpg-files/#comments</comments>
		<pubDate>Fri, 15 May 2009 15:32:06 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[C#]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://www.wengkien.com/?p=45</guid>
		<description><![CDATA[When you need to retrieve all the files from specific directory, here is the one of the effective sample that will retrieve all the &#8220;JPEG&#8221; files which using less memory compare to conventional way. Code // Create stack instance static Stack directoryStack = new Stack(); static void Main(string[] args) { Console.WriteLine("Application to find all the [...]]]></description>
			<content:encoded><![CDATA[<p>When you need to retrieve all the files from specific directory, here is the one of the effective sample that will retrieve all the &#8220;JPEG&#8221; files which using less memory compare to conventional way.</p>
<p><strong>Code</strong><br />
<code><br />
// Create stack instance<br />
static Stack<DirectoryInfo> directoryStack = new Stack<DirectoryInfo>();</p>
<p>static void Main(string[] args)<br />
{<br />
   Console.WriteLine("Application to find all the JPG files");</p>
<p>   Console.WriteLine("=====================================");<br />
   Console.Write("Location:");</p>
<p>   string location = Console.ReadLine();</p>
<p>   // Delete file.txt<br />
   if (File.Exists(@"C:\file.txt"))<br />
      File.Delete(@"C:\file.txt");</p>
<p>   // Create DirectoryInfo instance<br />
      DirectoryInfo di = new DirectoryInfo(location);</p>
<p>   // Add valid DirectoryInfo instance into stack<br />
   if (di != null)<br />
      directoryStack.Push(di);</p>
<p>   // stop looping when directoryStack items is equal to 0<br />
   while (directoryStack.Count != 0)<br />
      GetFiles();</p>
<p>   Console.WriteLine("Success");<br />
   Console.ReadLine();<br />
}</p>
<p>// Get and write all the "JPEG" files<br />
static void GetFiles()<br />
{<br />
   StreamWriter sw = System.IO.File.AppendText(@"C:\file.txt");</p>
<p>   // Get latest Directory Information from stack<br />
   DirectoryInfo currentDirectory = directoryStack.Pop();</p>
<p>   // Get all sub directories<br />
   DirectoryInfo[] getDirectories = currentDirectory.GetDirectories();</p>
<p>   // Add all sub directories into stack<br />
   for (int i = 0; i < getDirectories.Length; i++)<br />
      directoryStack.Push(getDirectories[i]);</p>
<p>   // Get all jpg files<br />
   FileInfo[] getFilesInfo = currentDirectory.GetFiles("*.jpg");</p>
<p>   // Write jpg full details into file.txt<br />
   for (int j = 0; j < getFilesInfo.Length; j++)<br />
      sw.WriteLine(getFilesInfo[j].FullName);</p>
<p>   sw.Close();<br />
}<br />
</code></p>
<p>Download full script file: <a href="http://wengkien.com/files/Load_files_using_stack.cs">Load_files_using_stack.cs</a></p>
<p>You also can using .NET library to search files that allocated within the directory and subdirectories.</p>
<p><strong>code</strong><br />
<code><br />
string searchLocation = @"C:\Program Files\";</p>
<p>// Get directory information<br />
DirectoryInfo searchDirectory = new DirectoryInfo(searchLocation);</p>
<p>// Get all the "JPEG" within directory and it's subdirectories<br />
FileInfo[] getFiles = searchDirectory.GetFiles("*.jpg", SearchOption.AllDirectories);</p>
<p>// Retrieve all match files information<br />
for (int i = 0; i < getFiles.Length; i++)<br />
   Console.WriteLine(getFiles[i].FullName);</p>
<p>Console.ReadLine();<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.wengkien.com/2009/05/15/using-stack-to-retrieve-all-the-jpg-files/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

