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)
' Recursively call this routine on all subfolders
For Each foldername As String In My.Computer.FileSystem.GetDirectories(folder)