<?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>eXclusiveMinds &#187; File Handling</title>
	<atom:link href="http://eXclusiveMinds.com/tag/file-handling/feed/" rel="self" type="application/rss+xml" />
	<link>http://eXclusiveMinds.com</link>
	<description>eXclusive resource for programmers, developers and designers</description>
	<lastBuildDate>Sat, 13 Mar 2010 23:28:44 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Tutorial about loading text from file and assigning it to string.</title>
		<link>http://eXclusiveMinds.com/2009/12/02/tutorial-about-loading-text-from-file-and-assigning-it-to-string/</link>
		<comments>http://eXclusiveMinds.com/2009/12/02/tutorial-about-loading-text-from-file-and-assigning-it-to-string/#comments</comments>
		<pubDate>Tue, 01 Dec 2009 19:53:58 +0000</pubDate>
		<dc:creator>eXclusiveMinds</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[File Handling]]></category>

		<guid isPermaLink="false">http://eXclusiveMinds.com/?p=115</guid>
		<description><![CDATA[This is simple example that demonstrates how can we load text string and assign it to variable of type string.  By using method &#8220;StreamReader.Re­adToEnd&#8221; we can load text from given file.  
i. Create instance of class StreamReader
ii. Pass filepath as a parameter
iii. Youcan also pass Stream Instance to constructor.
iv. Call ReadToEnd();
v. close stream
UTF-8 [...]]]></description>
			<content:encoded><![CDATA[<p>This is simple example that demonstrates how can we load text string and assign it to variable of type string.  By using method &#8220;StreamReader.Re­adToEnd&#8221; we can load text from given file.  <span id="more-115"></span><br />
i. Create instance of class StreamReader<br />
ii. Pass filepath as a parameter<br />
iii. Youcan also pass Stream Instance to constructor.<br />
iv. Call ReadToEnd();<br />
v. close stream<br />
UTF-8 is default file encoding for StreamReader.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;"><span style="color: #008080; font-style: italic;">// Create instance of StreamReader</span>
StreamReader myStreamReader <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StreamReader<span style="color: #000000;">&#40;</span>myFilePath<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
<span style="color: #FF0000;">string</span> text <span style="color: #008000;">=</span> myStreamReader.<span style="color: #0000FF;">ReadToEnd</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> 
myStreamReader.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<img src="http://eXclusiveMinds.Com/?ak_action=api_record_view&id=115&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://eXclusiveMinds.com/2009/12/02/tutorial-about-loading-text-from-file-and-assigning-it-to-string/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to get File Attributes in C#</title>
		<link>http://eXclusiveMinds.com/2008/10/26/how-to-get-file-attributes-in-c/</link>
		<comments>http://eXclusiveMinds.com/2008/10/26/how-to-get-file-attributes-in-c/#comments</comments>
		<pubDate>Sun, 26 Oct 2008 00:29:52 +0000</pubDate>
		<dc:creator>eXclusiveMinds</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[File Handling]]></category>

		<guid isPermaLink="false">http://eXclusiveMinds.com/?p=197</guid>
		<description><![CDATA[Following example demonstrates how to get or set file attributes; how to add attributes and how to remove attributes from current ones.
Get file attributes
We can get file attributes by using static method File.GetAttri­butes. This method returns FileAttributes which is bitwise combination of the file attribute flags.

string myfilePath = @&#34;c:\sample.txt&#34;;
FileAttributes attributes = File.GetAttributes&#40;myfilePath&#41;;

Set file attributes
We can [...]]]></description>
			<content:encoded><![CDATA[<p>Following example demonstrates how to get or set file attributes; how to add attributes and how to remove attributes from current ones.<span id="more-197"></span></p>
<h2>Get file attributes</h2>
<p>We can get file attributes by using static method <strong><a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.file.getattributes.aspx">File.GetAttri­butes</a></strong>. This method returns <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.fileattributes.aspx">FileAttributes</a> which is bitwise combination of the file attribute flags.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;"><span style="color: #FF0000;">string</span> myfilePath <span style="color: #008000;">=</span> <span style="color: #666666;">@&quot;c:\sample.txt&quot;</span><span style="color: #008000;">;</span>
FileAttributes attributes <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<h2>Set file attributes</h2>
<p>We can set file attributes by using static method <strong><a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.file.setattributes.aspx">File.SetAttri­butes</a></strong>. Parameter of the method is a bitwise combination of <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.fileattributes.aspx">FileAttributes</a> enumeration.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">File.<span style="color: #0000FF;">SetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath, FileAttributes.<span style="color: #0000FF;">Normal</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
File.<span style="color: #0000FF;">SetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath, FileAttributes.<span style="color: #0000FF;">Archive</span> <span style="color: #008000;">|</span>
                             FileAttributes.<span style="color: #0600FF;">ReadOnly</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p><strong>Example showing how we can check for file attributes</strong><br />
First we can check whether file has any attribute or not and get current attributes then we can apply bitwise AND (&#038;) operator with other attributes.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;"><span style="color: #FF0000;">bool</span> isReadOnly <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> FileAttributes.<span style="color: #0600FF;">ReadOnly</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> FileAttributes.<span style="color: #0600FF;">ReadOnly</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">bool</span> isHidden <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> FileAttributes.<span style="color: #0000FF;">Hidden</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> FileAttributes.<span style="color: #0000FF;">Hidden</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">bool</span> isArchive <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> FileAttributes.<span style="color: #0000FF;">Archive</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> FileAttributes.<span style="color: #0000FF;">Archive</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">bool</span> isSystem <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> FileAttributes.<span style="color: #000000;">System</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">==</span> FileAttributes.<span style="color: #000000;">System</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p><strong>Example showing how we can add attributes to file</strong><br />
First we can get current attributes of file then we can use bitwise OR (|) with other attributes.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">File.<span style="color: #0000FF;">SetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath, File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">|</span> FileAttributes.<span style="color: #0000FF;">Hidden</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
File.<span style="color: #0000FF;">SetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath, File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">|</span> <span style="color: #000000;">&#40;</span>FileAttributes.<span style="color: #0000FF;">Archive</span> <span style="color: #008000;">|</span>
                                                             FileAttributes.<span style="color: #0600FF;">ReadOnly</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p><strong>Example showing how we can Clear/Delete file attributes</strong><br />
First we can get current attributes of file then we can use the operator (AND (&#038;)) with a mask.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">File.<span style="color: #0000FF;">SetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath, File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> ~FileAttributes.<span style="color: #0000FF;">Hidden</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
File.<span style="color: #0000FF;">SetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath, File.<span style="color: #0000FF;">GetAttributes</span><span style="color: #000000;">&#40;</span>myfilePath<span style="color: #000000;">&#41;</span> <span style="color: #008000;">&amp;</span> ~<span style="color: #000000;">&#40;</span>FileAttributes.<span style="color: #0000FF;">Archive</span> <span style="color: #008000;">|</span>
                                                              FileAttributes.<span style="color: #0600FF;">ReadOnly</span><span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<img src="http://eXclusiveMinds.Com/?ak_action=api_record_view&id=197&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://eXclusiveMinds.com/2008/10/26/how-to-get-file-attributes-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get File Time C#</title>
		<link>http://eXclusiveMinds.com/2008/10/13/get-file-time/</link>
		<comments>http://eXclusiveMinds.com/2008/10/13/get-file-time/#comments</comments>
		<pubDate>Mon, 13 Oct 2008 00:26:08 +0000</pubDate>
		<dc:creator>eXclusiveMinds</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[File Handling]]></category>

		<guid isPermaLink="false">http://eXclusiveMinds.com/?p=195</guid>
		<description><![CDATA[Following example demonstrate how we can get file time info, it&#8217;s creation time, modification time or last access time.

We can get it in two ways:
i. Using File class.
ii. Using FileInfo class.
Example File class (Getting file time)
If we need to get only one specific time we can use File class.

// Getting local time
DateTime creationTimeLocal = File.GetCreationTime&#40;@&#34;D:\sample.txt&#34;&#41;;
DateTime [...]]]></description>
			<content:encoded><![CDATA[<p>Following example demonstrate how we can get file time info, it&#8217;s creation time, modification time or last access time.<br />
<span id="more-195"></span><br />
We can get it in two ways:<br />
i. Using <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.file_members.aspx">File</a> class.<br />
ii. Using <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.fileinfo_members.aspx">FileInfo</a> class.</p>
<h2>Example File class (Getting file time)</h2>
<p>If we need to get only one specific time we can use <strong><a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.file_members.aspx">File</a> class</strong>.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;"><span style="color: #008080; font-style: italic;">// Getting local time</span>
DateTime creationTimeLocal <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">GetCreationTime</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DateTime lastWriteTimeLocal <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">GetLastWriteTime</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DateTime lastAccessTimeLocal <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">GetLastAccessTime</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Getting UTC time</span>
DateTime fileCreationTimeUtc <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">GetCreationTimeUtc</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DateTime fileLastWriteTimeUtc <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">GetLastWriteTimeUtc</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
DateTime fileLastAccessTimeUtc <span style="color: #008000;">=</span> File.<span style="color: #0000FF;">GetLastAccessTimeUtc</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<h2>Example FileInfo class (Getting file time)</h2>
<p>If we need to get multiple file time we ca use <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.fileinfo_members.aspx">FileInfo</a> class.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">FileInfo myFileInfo <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileInfo<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Getting local time</span>
DateTime creationTimeLocal <span style="color: #008000;">=</span> myFileInfo.<span style="color: #0000FF;">CreationTime</span><span style="color: #008000;">;</span>
DateTime lastWriteTimeLocal <span style="color: #008000;">=</span> myFileInfo.<span style="color: #0000FF;">LastWriteTime</span><span style="color: #008000;">;</span>
DateTime lastAccessTimeLocal <span style="color: #008000;">=</span> myFileInfo.<span style="color: #0000FF;">LastAccessTime</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #008080; font-style: italic;">// Getting UTC time</span>
DateTime creationTimeUtc <span style="color: #008000;">=</span> myFileInfo.<span style="color: #0000FF;">CreationTimeUtc</span><span style="color: #008000;">;</span>
DateTime lastWriteTimeUtc <span style="color: #008000;">=</span> myFileInfo.<span style="color: #0000FF;">LastWriteTimeUtc</span><span style="color: #008000;">;</span>
DateTime lastAccessTimeUtc <span style="color: #008000;">=</span> myFileInfo.<span style="color: #0000FF;">LastAccessTimeUtc</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<img src="http://eXclusiveMinds.Com/?ak_action=api_record_view&id=195&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://eXclusiveMinds.com/2008/10/13/get-file-time/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get Application Directory in C#</title>
		<link>http://eXclusiveMinds.com/2008/09/07/get-application-directory-in-c/</link>
		<comments>http://eXclusiveMinds.com/2008/09/07/get-application-directory-in-c/#comments</comments>
		<pubDate>Sun, 07 Sep 2008 00:32:24 +0000</pubDate>
		<dc:creator>eXclusiveMinds</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[File Handling]]></category>

		<guid isPermaLink="false">http://eXclusiveMinds.com/?p=200</guid>
		<description><![CDATA[This example demonstrates how can we get application or assembly folder.
Geting Directory of the windows forms application
To get directory we need to use static property ExecutablePath from  Application in System.Window­s.Forms namespace. 
By using static method GetDirectoryName of Path class we can get only the folder part of the path.

string appPath = Path.GetDirectoryName&#40;Application.ExecutablePath&#41;;

Getting Directory of [...]]]></description>
			<content:encoded><![CDATA[<p>This example demonstrates how can we get application or assembly folder.<span id="more-200"></span></p>
<h2>Geting Directory of the windows forms application</h2>
<p>To get directory we need to use static property <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.windows.forms.application.executablepath.aspx">ExecutablePath</a> from  Application in System.Window­s.Forms namespace. </p>
<p>By using static method <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.path.getdirectoryname.aspx">GetDirectoryName</a> of Path class we can get only the folder part of the path.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;"><span style="color: #FF0000;">string</span> appPath <span style="color: #008000;">=</span> Path.<span style="color: #0000FF;">GetDirectoryName</span><span style="color: #000000;">&#40;</span>Application.<span style="color: #0000FF;">ExecutablePath</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<h2>Getting Directory of loaded assembly </h2>
<p>By using the method Assembly.<a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getexecutingassembly.aspx">GetE­xecutingAssem­bly</a> we can get assembly of currently executing program.<br />
By using method Assembly.<a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.getassembly.aspx">GetAs­sembly</a> we can get assembly in which the specified class is defined. Finally, we can get a assembly file path using Assembly.<a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.reflection.assembly.codebase.aspx">CodeBase</a> property.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;"><span style="color: #FF0000;">string</span> appPath <span style="color: #008000;">=</span> Path.<span style="color: #0000FF;">GetDirectoryName</span><span style="color: #000000;">&#40;</span>
                     Assembly.<span style="color: #0000FF;">GetAssembly</span><span style="color: #000000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #000000;">&#40;</span>MyClassName<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span>.<span style="color: #0000FF;">CodeBase</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<img src="http://eXclusiveMinds.Com/?ak_action=api_record_view&id=200&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://eXclusiveMinds.com/2008/09/07/get-application-directory-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Delete All Files in directory using C#</title>
		<link>http://eXclusiveMinds.com/2008/08/16/delete-all-files-in-directory-using-c/</link>
		<comments>http://eXclusiveMinds.com/2008/08/16/delete-all-files-in-directory-using-c/#comments</comments>
		<pubDate>Sat, 16 Aug 2008 00:36:37 +0000</pubDate>
		<dc:creator>eXclusiveMinds</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[File Handling]]></category>

		<guid isPermaLink="false">http://eXclusiveMinds.com/?p=206</guid>
		<description><![CDATA[Following examples shows how to delete all files from given a folder in C#.

Example

using System.IO;
string&#91;&#93; filePaths = Directory.GetFiles&#40;@&#34;c:\MyDir\&#34;&#41;;
foreach &#40;string filePath in filePaths&#41;
  File.Delete&#40;filePath&#41;;

Example (using anonymous method)

&#160;
Array.ForEach&#40;Directory.GetFiles&#40;@&#34;c:\MyDir\&#34;&#41;,
              delegate&#40;string path&#41; &#123; File.Delete&#40;path&#41;; &#125;&#41;;

]]></description>
			<content:encoded><![CDATA[<p>Following examples shows how to delete all files from given a folder in C#.<br />
<span id="more-206"></span><br />
<strong>Example</strong></p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;"><span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> filePaths <span style="color: #008000;">=</span> Directory.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;c:\MyDir\&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">foreach</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> filePath <span style="color: #0600FF;">in</span> filePaths<span style="color: #000000;">&#41;</span>
  File.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span>filePath<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p><strong>Example (using anonymous method)</strong></p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">&nbsp;
Array.<span style="color: #0600FF;">ForEach</span><span style="color: #000000;">&#40;</span>Directory.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;c:\MyDir\&quot;</span><span style="color: #000000;">&#41;</span>,
              <span style="color: #FF0000;">delegate</span><span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> path<span style="color: #000000;">&#41;</span> <span style="color: #000000;">&#123;</span> File.<span style="color: #0000FF;">Delete</span><span style="color: #000000;">&#40;</span>path<span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span> <span style="color: #000000;">&#125;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<img src="http://eXclusiveMinds.Com/?ak_action=api_record_view&id=206&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://eXclusiveMinds.com/2008/08/16/delete-all-files-in-directory-using-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Get Files from Directory using C#</title>
		<link>http://eXclusiveMinds.com/2008/08/06/get-files-from-directory-using-c/</link>
		<comments>http://eXclusiveMinds.com/2008/08/06/get-files-from-directory-using-c/#comments</comments>
		<pubDate>Wed, 06 Aug 2008 00:39:03 +0000</pubDate>
		<dc:creator>eXclusiveMinds</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[File Handling]]></category>

		<guid isPermaLink="false">http://eXclusiveMinds.com/?p=209</guid>
		<description><![CDATA[We&#8217;ve this example which demonstrate how to get list of file names from a directory which includs subdirectories also. We can always filter the list of file by specific extension.

We use static method Directory.Get­Files to get file names from the specified directory. Lets get list of files and subfolders in C:\MyDir:
Get files from directory
Method Directory.GetFiles [...]]]></description>
			<content:encoded><![CDATA[<p>We&#8217;ve this example which demonstrate how to get list of file names from a directory which includs subdirectories also. We can always filter the list of file by specific extension.<br />
<span id="more-209"></span><br />
We use static method Directory.Get­Files to get file names from the specified directory. Lets get list of files and subfolders in C:\MyDir:</p>
<p><strong>Get files from directory</strong><br />
Method Directory.GetFiles returns string array with files names (full paths).</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">&nbsp;
<span style="color: #0600FF;">using</span> <span style="color: #008080;">System.IO</span><span style="color: #008000;">;</span>
&nbsp;
<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> filePaths <span style="color: #008000;">=</span> Directory.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;c:\MyDir\&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p><strong>Get files from directory (with specified extension)</strong><br />
You can specify search pattern. You can use wildcard specifiers in the search pattern, e.g. &#8220;.gif&#8221; to select files with the extension or &#8220;a*&#8221; to select files beginning with letter &#8220;a&#8221;.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">&nbsp;
<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> filePaths <span style="color: #008000;">=</span> Directory.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;c:\MyDir\&quot;</span>, <span style="color: #666666;">&quot;*.gif&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p><strong>Get files from directory (including all subdirectories)</strong><br />
If you want to search also in subfolders use parameter SearchOption.A­llDirectories.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">&nbsp;
<span style="color: #FF0000;">string</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> filePaths <span style="color: #008000;">=</span> Directory.<span style="color: #0000FF;">GetFiles</span><span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;c:\MyDir\&quot;</span>, <span style="color: #666666;">&quot;*.gif&quot;</span>,
                                         SearchOption.<span style="color: #0000FF;">AllDirectories</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<img src="http://eXclusiveMinds.Com/?ak_action=api_record_view&id=209&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://eXclusiveMinds.com/2008/08/06/get-files-from-directory-using-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to open file using file stream in C#</title>
		<link>http://eXclusiveMinds.com/2008/07/02/how-to-open-file-using-file-stream-in-c/</link>
		<comments>http://eXclusiveMinds.com/2008/07/02/how-to-open-file-using-file-stream-in-c/#comments</comments>
		<pubDate>Wed, 02 Jul 2008 00:44:50 +0000</pubDate>
		<dc:creator>eXclusiveMinds</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[File Handling]]></category>

		<guid isPermaLink="false">http://eXclusiveMinds.com/?p=215</guid>
		<description><![CDATA[Following example shows how can we open files for writing or reading and how do we load and save file using class FileStream in C#.  For this we simply create instance of FileStream class with FileMode and FileAccess enumerations as parameters.
FileStream Usage: 
Following code is used when opening the file using FileStream. Always close [...]]]></description>
			<content:encoded><![CDATA[<p>Following example shows how can we open files for writing or reading and how do we load and save file using class FileStream in C#.  For this we simply create instance of <strong><a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.filestream.aspx">FileStream</a></strong> class with <strong><a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.filemode.aspx">FileMode</a></strong> and <strong><a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/4z36sx0f.aspx">FileAccess</a></strong> enumerations as parameters.<span id="more-215"></span></p>
<h2>FileStream Usage: </h2>
<p>Following code is used when opening the file using FileStream. Always close stream; if we do not close the stream this may take some time to be file again accessible.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">&nbsp;
FileStream fs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;c:\sample.txt&quot;</span>, FileMode.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #0600FF;">try</span>
<span style="color: #000000;">&#123;</span>
  <span style="color: #008080; font-style: italic;">//coading</span>
<span style="color: #000000;">&#125;</span>
<span style="color: #0600FF;">finally</span>
<span style="color: #000000;">&#123;</span>
  fs.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></td></tr></table></div>

<p><strong> Common Scenarios:</strong></p>
<p>Opening file for write and read both.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">FileStream fs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span>, FileMode.<span style="color: #0000FF;">Open</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p>Opening file for reading</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">FileStream fs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span>, FileMode.<span style="color: #0000FF;">Open</span>, FileAccess.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p>Opening file for writing.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">FileStream fs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span>, FileMode.<span style="color: #0000FF;">Open</span>, FileAccess.<span style="color: #0000FF;">Write</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p>Opening file for writing and creating new one if it doesn&#8217;t exist.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">FileStream fs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span>, FileMode.<span style="color: #0000FF;">Append</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p>Creating new file for write and read / Overwrite if file already exists</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">FileStream fs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span>, FileMode.<span style="color: #0000FF;">Create</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<p>Creating new file for write/read and Throw exception if It&#8217;s already exists</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;">FileStream fs <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span><span style="color: #666666;">@&quot;D:\sample.txt&quot;</span>, FileMode.<span style="color: #0000FF;">CreateNew</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span></pre></div></td></tr></table></div>

<img src="http://eXclusiveMinds.Com/?ak_action=api_record_view&id=215&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://eXclusiveMinds.com/2008/07/02/how-to-open-file-using-file-stream-in-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>How to safely read file stream using C#</title>
		<link>http://eXclusiveMinds.com/2008/06/30/how-to-safely-read-file-stream-using-c/</link>
		<comments>http://eXclusiveMinds.com/2008/06/30/how-to-safely-read-file-stream-using-c/#comments</comments>
		<pubDate>Mon, 30 Jun 2008 00:42:27 +0000</pubDate>
		<dc:creator>eXclusiveMinds</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[.NET]]></category>
		<category><![CDATA[C#]]></category>
		<category><![CDATA[File Handling]]></category>

		<guid isPermaLink="false">http://eXclusiveMinds.com/?p=212</guid>
		<description><![CDATA[Following example shows how do we safely read the file using FileStream in C#.
By calling FileStream.Read method in a loop we can gurantee whole file is correctly read.

Example showing how can we read file safely using FileStream
i. Create FileStream to open the file for reading.
ii. Call FileStream.Read until all the content from file is read.
ii. [...]]]></description>
			<content:encoded><![CDATA[<p>Following example shows how do we <strong>safely read the file using <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.filestream.aspx">FileStream</a> in C#</strong>.<br />
By calling <a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.filestream.read.aspx">FileStream.Read</a> method in a loop we can gurantee whole file is correctly read.<br />
<span id="more-212"></span></p>
<h2>Example showing how can we read file safely using FileStream</h2>
<p>i. Create <strong><a href="http://exclusiveminds.com/filestream-open-file/">FileStream</a> </strong>to open the file for reading.<br />
ii. Call <strong><a rel="nofollow" href="http://msdn2.microsoft.com/en-us/library/system.io.filestream.read.aspx">FileStream.Read</a></strong> until all the content from file is read.<br />
ii. Close the file stream.</p>

<div class="wp_syntax"><table><tr><td><div class="code"><pre class="csharp" style="font-family:Consolas; monospace;"> <span style="color: #0600FF;">public</span> <span style="color: #0600FF;">static</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> ReadingFileSafely<span style="color: #000000;">&#40;</span><span style="color: #FF0000;">string</span> path<span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span><span style="color: #000000;">&#93;</span> myBuffer<span style="color: #008000;">;</span>
    FileStream myStream <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> FileStream<span style="color: #000000;">&#40;</span>path, FileMode.<span style="color: #0000FF;">Open</span>, FileAccess.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #008080; font-style: italic;">//Start reading file to buffer</span>
    <span style="color: #0600FF;">try</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #FF0000;">int</span> len <span style="color: #008000;">=</span> <span style="color: #000000;">&#40;</span><span style="color: #FF0000;">int</span><span style="color: #000000;">&#41;</span>myStream.<span style="color: #0000FF;">Length</span><span style="color: #008000;">;</span>
        myBuffer <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #FF0000;">byte</span><span style="color: #000000;">&#91;</span>len<span style="color: #000000;">&#93;</span><span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> count<span style="color: #008000;">;</span>
        <span style="color: #FF0000;">int</span> total <span style="color: #008000;">=</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF;">while</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#40;</span>count <span style="color: #008000;">=</span> myStream.<span style="color: #0000FF;">Read</span><span style="color: #000000;">&#40;</span>myBuffer, total, len <span style="color: #008000;">-</span> total<span style="color: #000000;">&#41;</span><span style="color: #000000;">&#41;</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #000000;">&#41;</span>
            total <span style="color: #008000;">+=</span> count<span style="color: #008000;">;</span> 
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">finally</span>
    <span style="color: #000000;">&#123;</span>
        myStream.<span style="color: #0000FF;">Close</span><span style="color: #000000;">&#40;</span><span style="color: #000000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #000000;">&#125;</span>
    <span style="color: #0600FF;">return</span> myBuffer<span style="color: #008000;">;</span>
<span style="color: #000000;">&#125;</span></pre></div></td></tr></table></div>

<img src="http://eXclusiveMinds.Com/?ak_action=api_record_view&id=212&type=feed" alt="" />]]></content:encoded>
			<wfw:commentRss>http://eXclusiveMinds.com/2008/06/30/how-to-safely-read-file-stream-using-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
