forked from gitextensions/gitextensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFileInfoExtensions.cs
31 lines (28 loc) · 919 Bytes
/
FileInfoExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
using System;
using System.IO;
namespace GitCommands
{
public static class FileInfoExtensions
{
/// <summary>
/// Remove all attributes that could cause the file to be read-only
/// and restores them later
/// </summary>
public static void MakeFileTemporaryWritable(string fileName, Action<string> writableAction)
{
var fileInfo = new FileInfo(fileName);
if (!fileInfo.Exists)
{
//The file doesn't exist yet, no need to make it writable
writableAction(fileName);
return;
}
var oldAttributes = fileInfo.Attributes;
fileInfo.Attributes = FileAttributes.Normal;
writableAction(fileName);
fileInfo.Refresh();
if (fileInfo.Exists)
fileInfo.Attributes = oldAttributes;
}
}
}