Recursively Set All Folders & Files to NOT Read-Only

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!!!
 
K

Kai Brinkmann [MSFT]

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.
 
M

melo

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top