Recursively Set All Folders & Files to NOT Read-Only

  • Thread starter Thread starter melo
  • Start date Start date
M

melo

Hello,
I've been struggling with a function(s) to recursively set all folders and
files to NOT read-only. So, I thought I'd post this message.

What I need to do is: given a starting path, I need to recursively go
through all files and folders below the starting path and check if the file
or folder is read-only and, if so, set it to not read-only.

Any ideas?

Thanks!!!
 
The code for this is actually pretty straight-forward. Something like:

public void ParseFolders(string s_Folder)
string[] SubDirectories = Directory.GetDirectories(s_Folder);
if (SubDirectories.Length == 0)
{
// process this folder
}
else
{
for (int i = 0; i < SubDirectories.Length; i++)
{
ParseFolders(SubDirectories);
}
// process this folder
}
}

will recursively parse a directory tree rooted at path s_Folder. For each folder, you would simply have to check the attributes of the folder itself as well as those of each file it contains [which you can obtain using Directory.GetFiles]. Use File.GetAttributes to obtain the attributes. Whenever you encounter FileAttributes.ReadOnly use File.SetAttribute to set the attributes to FileAttributes.Archive (or some other member of the FileAttributes enumeration).

I hope this helps.
 
Thank you. I'll have a closer look at your solution. But, you're right, it does seem pretty straight forward now that I see your ideas.

The code for this is actually pretty straight-forward. Something like:

public void ParseFolders(string s_Folder)
string[] SubDirectories = Directory.GetDirectories(s_Folder);
if (SubDirectories.Length == 0)
{
// process this folder
}
else
{
for (int i = 0; i < SubDirectories.Length; i++)
{
ParseFolders(SubDirectories);
}
// process this folder
}
}

will recursively parse a directory tree rooted at path s_Folder. For each folder, you would simply have to check the attributes of the folder itself as well as those of each file it contains [which you can obtain using Directory.GetFiles]. Use File.GetAttributes to obtain the attributes. Whenever you encounter FileAttributes.ReadOnly use File.SetAttribute to set the attributes to FileAttributes.Archive (or some other member of the FileAttributes enumeration).

I hope this helps.
 
Back
Top