public static void MakeFileTemporaryWriteable(string fileName, Action<string> writeableAction)
{
var fileInfo = new FileInfo(fileName);
if (!fileInfo.Exists)
{
//The file doesn't exist yet, no need to make it writable
writeableAction(fileName);
return;
}
var oldAttributes = fileInfo.Attributes;
fileInfo.Attributes = FileAttributes.Normal;
writeableAction(fileName);
fileInfo.Refresh();
if (fileInfo.Exists)
fileInfo.Attributes = oldAttributes;
}