Avian Waves : Blog
Topic: All
Search Blog
 
Recent Comments
Easy Way to Change Permissions on the Windows Server Scheduled Tasks Folder (C:\Windows\Tasks) (22)
4Fingus: Thank you so much. You have saved me alot of...

Mystery of the Missing msnmsn.inf on XP SP3 (24)
jo w: Thanx m8, it's even brilliant for those who are...

Delegating "Power Options" Management to Limited Users in XP (6)
Alessandro Stillitano: Hi :)I can report that on a Lenovo T410i the...

Mystery of the Server 2008 + IIS7 + OLE = "MDAC Not Installed" Error (1)
User1: Great analysis. I really appreciate the way you...
Last 15 Posts
Archives
Posted By Timothy • Topic: Tech
Sep 20, 2006 11:34 PM EDT
One thing I love about the new VB.NET in Visual Studio 2005 is the “My” namespace, which offers tons of useful code for common tasks in a single easy to find namespace.  As good as the My.Computer.FileSystem namespace is, however, it is lacking a way to easily change attributes on files. 

I needed to recursively remove all Read-Only and Hidden attributes in a folder hierarchy in order to copy over (write over) those files using the My.Computer.FileSystem.CopyDirectory method.  This method will throw an exception if a file is marked as Read-Only.  So, I wrote the following code segment to remove the Read-Only and Hidden attributes for me.  Of course this can be easily modified to set other attributes too.

Public Sub RemoveReadOnlyAttributes(ByVal folder As String)
  ' Remove attribute on individual files
  For Each filename As String In My.Computer.FileSystem.GetFiles(folder)
    System.IO.File.SetAttributes(filename, IO.FileAttributes.Normal)
  Next

  ' Recursively call this routine on all subfolders
  For Each foldername As String In My.Computer.FileSystem.GetDirectories(folder)
    RemoveReadOnlyAttributes(foldername)
  Next
End Sub

Trackbacks :
Send trackbacks to this URL:
http://www.avianwaves.com/Blog/track.aspx?id=9
In your blog post, you must link back to my post using this URL:
http://www.avianwaves.com/Blog/default.aspx?id=9
Comments :
A very elegant piece of code!
Comment By Clint At 1/28/2008 2:41 PM EDT PermaLink
Hi,

Just an FYI:

I changed this line:
For Each foldername As String In My.Computer.FileSystem.GetDirectories(folder)


TO:
For Each foldername As String In My.Computer.FileSystem.GetDirectories(folder, Microsoft.VisualBasic.FileIO.SearchOption.SearchAllSubDirectories)

Seems to behave better now.
Comment By Cj At 7/21/2010 3:47 PM EDT PermaLink
Add Comment :
Name :
Email :
URL (Optional) :
       
Comments :
Allowed BBCode Tags : LINK (URL), BOLD, ITALIC, UNDERLINE, QUOTE


By posting a comment to this blog, I agree to the blog rules.

This Avian Waves blog is powered by a modified version of Presstopia Blog




Avian Waves Logo