Saturday, May 30, 2009

Set a file as read-only

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);
}
}

No comments:

Post a Comment