views:

90

answers:

1

Hi all.

I have a folder with some folders in it and some files in those folders. It so happens, that those file may be readonly which is not acceptable for me.

The questions is do I have to write the code to recursively unset readonly on all files? It's not difficult to write but is there any standard .NET way to do it?

My current solution:

private static void SetReadOnly( string path, bool readOnly )
{
    foreach ( string directory in Directory.GetDirectories( path ) )
    {
        DirectoryInfo di = new DirectoryInfo( directory );
        if ( readOnly )
        {
            di.Attributes &= FileAttributes.ReadOnly;
        }
        else
        {
            di.Attributes ^= FileAttributes.ReadOnly;
        }

        SetReadOnly( directory, readOnly );
    }

    foreach ( string file in Directory.GetFiles( path ) )
    {
        FileInfo fi = new FileInfo( file );
        if ( readOnly )
        {
            fi.Attributes &= FileAttributes.ReadOnly;
        }
        else
        {
            fi.Attributes ^= FileAttributes.ReadOnly;
        }
    }
}
A: 

No, there's no built-in method for that. You have to write it yourself, and your existing code looks like it would do the job.

Mattias S