<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	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/"
		>
<channel>
	<title>Comments on: Clearing the read-only flag on a file in C#</title>
	<atom:link href="http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/</link>
	<description>Mostly tech talk</description>
	<lastBuildDate>Tue, 10 Aug 2010 10:51:02 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: Kevin P. Rice</title>
		<link>http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/comment-page-1/#comment-703</link>
		<dc:creator>Kevin P. Rice</dc:creator>
		<pubDate>Tue, 25 Aug 2009 06:19:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/#comment-703</guid>
		<description>The easiest solution is:

myFile.IsReadOnly = false;

...or, as ripper234 wrote:

myFile.Attributes &amp;= ~FileAttributes.ReadOnly;

Neither of these methods require a pre-test as they ALWAYS remove read-only status.

--------------
Scott&#039;s solution (using the -= assignment operator) is dangerous for two reasons:
(1) It works ONLY IF the ReadOnly attribute is set, thus a test is required beforehand.
(2) It is performing a subtract operation, which is not is not the best choice when working with binary flags. The subtract operation works if condition 1 (above) is true, but additional subtract operations will ALTER OTHER BITS in the FileAttributes field!

To appropriately manipulate binary flags, you should only use bitwise (binary) operators:

&amp; (bitwise AND) - clears bits
&#124; (bitwise OR) - sets bits
^ (bitwise XOR) - flip-flops bits
~ (bitwise negation) - flip-flops all bits

You can also use the corresponding compound assignment counterparts:

&amp;= (bitwise AND and assign)
&#124;= (bitwise OR and assign)
^= (bitwise XOR and assign)

Not appropriate for flags, but appropriate for other binary operations:

&lt;&gt; (shift right) - moves bits to the right (same as divide by n^2, but faster)

-----------

How ripper234&#039;s method works:

FileAttributes.ReadOnly = 0x0000000000000001

In order to use the bitwise AND operator to clear a bit, we need the complement of this:

~FileAttributes.ReadOnly = 0x1111111111111110

Next, we perform the bitwise AND, then assign it back:

myFile.Attributes = myFile.Attributes &amp; ~FileAttributes.ReadOnly;

The &amp;= operator is shorthand for the above and yields:

myFire.Attributes &amp;= ~FileAttributes.ReadOnly;</description>
		<content:encoded><![CDATA[<p>The easiest solution is:</p>
<p>myFile.IsReadOnly = false;</p>
<p>&#8230;or, as ripper234 wrote:</p>
<p>myFile.Attributes &amp;= ~FileAttributes.ReadOnly;</p>
<p>Neither of these methods require a pre-test as they ALWAYS remove read-only status.</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8211;<br />
Scott&#8217;s solution (using the -= assignment operator) is dangerous for two reasons:<br />
(1) It works ONLY IF the ReadOnly attribute is set, thus a test is required beforehand.<br />
(2) It is performing a subtract operation, which is not is not the best choice when working with binary flags. The subtract operation works if condition 1 (above) is true, but additional subtract operations will ALTER OTHER BITS in the FileAttributes field!</p>
<p>To appropriately manipulate binary flags, you should only use bitwise (binary) operators:</p>
<p>&amp; (bitwise AND) &#8211; clears bits<br />
| (bitwise OR) &#8211; sets bits<br />
^ (bitwise XOR) &#8211; flip-flops bits<br />
~ (bitwise negation) &#8211; flip-flops all bits</p>
<p>You can also use the corresponding compound assignment counterparts:</p>
<p>&amp;= (bitwise AND and assign)<br />
|= (bitwise OR and assign)<br />
^= (bitwise XOR and assign)</p>
<p>Not appropriate for flags, but appropriate for other binary operations:</p>
<p>&lt;&gt; (shift right) &#8211; moves bits to the right (same as divide by n^2, but faster)</p>
<p>&#8212;&#8212;&#8212;&#8211;</p>
<p>How ripper234&#8217;s method works:</p>
<p>FileAttributes.ReadOnly = 0&#215;0000000000000001</p>
<p>In order to use the bitwise AND operator to clear a bit, we need the complement of this:</p>
<p>~FileAttributes.ReadOnly = 0&#215;1111111111111110</p>
<p>Next, we perform the bitwise AND, then assign it back:</p>
<p>myFile.Attributes = myFile.Attributes &amp; ~FileAttributes.ReadOnly;</p>
<p>The &amp;= operator is shorthand for the above and yields:</p>
<p>myFire.Attributes &amp;= ~FileAttributes.ReadOnly;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: ripper234</title>
		<link>http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/comment-page-1/#comment-697</link>
		<dc:creator>ripper234</dc:creator>
		<pubDate>Mon, 16 Mar 2009 10:44:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/#comment-697</guid>
		<description>Scott&#039;s solution does not consistently work. I&#039;m not sure why exactly, but when applied repeatedly to a file it turned read-only again.

This works:

FileInfo info = new FileInfo(file);
info.Attributes &amp;= ~FileAttributes.ReadOnly;</description>
		<content:encoded><![CDATA[<p>Scott&#8217;s solution does not consistently work. I&#8217;m not sure why exactly, but when applied repeatedly to a file it turned read-only again.</p>
<p>This works:</p>
<p>FileInfo info = new FileInfo(file);<br />
info.Attributes &amp;= ~FileAttributes.ReadOnly;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mark Scott</title>
		<link>http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/comment-page-1/#comment-612</link>
		<dc:creator>Mark Scott</dc:creator>
		<pubDate>Fri, 07 Nov 2008 12:34:34 +0000</pubDate>
		<guid isPermaLink="false">http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/#comment-612</guid>
		<description>For changing the flag, this is a little prettier
 // flip the read only attribute
   myFile.Attributes ^= FileAttributes.ReadOnly;</description>
		<content:encoded><![CDATA[<p>For changing the flag, this is a little prettier<br />
 // flip the read only attribute<br />
   myFile.Attributes ^= FileAttributes.ReadOnly;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Deniz</title>
		<link>http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/comment-page-1/#comment-609</link>
		<dc:creator>Deniz</dc:creator>
		<pubDate>Tue, 21 Oct 2008 12:33:24 +0000</pubDate>
		<guid isPermaLink="false">http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/#comment-609</guid>
		<description>In case someone needs the VB.NET Code:

Dim myFile As FileInfo =  New FileInfo(&quot;c:\myfile.txt&quot;) 
 
If myFile.IsReadOnly ThenThen
  &#039; remove the read only attribute
  myFile.IsReadOnly = False
End If</description>
		<content:encoded><![CDATA[<p>In case someone needs the VB.NET Code:</p>
<p>Dim myFile As FileInfo =  New FileInfo(&#8220;c:\myfile.txt&#8221;) </p>
<p>If myFile.IsReadOnly ThenThen<br />
  &#8216; remove the read only attribute<br />
  myFile.IsReadOnly = False<br />
End If</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: omatase</title>
		<link>http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/comment-page-1/#comment-606</link>
		<dc:creator>omatase</dc:creator>
		<pubDate>Tue, 26 Aug 2008 16:37:42 +0000</pubDate>
		<guid isPermaLink="false">http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/#comment-606</guid>
		<description>awesome input, thanks Scott!</description>
		<content:encoded><![CDATA[<p>awesome input, thanks Scott!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Scott</title>
		<link>http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/comment-page-1/#comment-605</link>
		<dc:creator>Scott</dc:creator>
		<pubDate>Tue, 26 Aug 2008 14:24:11 +0000</pubDate>
		<guid isPermaLink="false">http://www.del337ed.com/blog/index.php/2007/09/05/clearing-the-read-only-flag-on-a-file-in-c/#comment-605</guid>
		<description>myFile.Attributes -= FileAttributes.ReadOnly; works as well</description>
		<content:encoded><![CDATA[<p>myFile.Attributes -= FileAttributes.ReadOnly; works as well</p>
]]></content:encoded>
	</item>
</channel>
</rss>
