Author: Alessio Saltarin
URL: http://www.bytemycode.com/snippets/snippet/311/
Set the read-only attribute of a file to true or false
// Set the read-only attribute of a file to true or false
// <summary>
// Set the attribute read only of a file
// </summary>
// <param name="fullName">Full path of file</param>
// <param name="readOnly">If true, the file will be set readonly. Else will be set writable</param>
public void SetReadOnly(string fullName, bool readOnly)
{
FileInfo filePath = new FileInfo(fullName);
try
{
if (filePath.Exists)
{
FileAttributes attr;
if (readOnly)
{
attr = (FileAttributes)
(filePath.Attributes | FileAttributes.ReadOnly);
}
else
{
attr = (FileAttributes)
(filePath.Attributes - FileAttributes.ReadOnly);
}
File.SetAttributes(filePath.FullName, attr);
}
}
catch (IOException e)
{
System.Diagnostics.Debug.WriteLine(e.Source);
System.Diagnostics.Debug.WriteLine(e.Message);
System.Diagnostics.Debug.WriteLine(e.StackTrace);
}
}










