Friday, January 23, 2009

C#: Update File attributes Readonly to normal mode

Here’s a nice little piece of code you can use to change file attributes of a particular file, its particularly useful when you’re trying to delete a read only file.



string savedCardFilePath = "My absolute file path";

if (File.Exists(savedCardFilePath))
{

// Remove readonly attribute off of the file if it exists before execution

if(File.GetAttributes(savedCardFilePath) == FileAttributes.ReadOnly)
File.SetAttributes(savedCardFilePath, FileAttributes.Normal);
File.Delete(savedCardFilePath);

}

No comments:

Post a Comment