This repository has been archived by the owner on Dec 18, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 224
/
FileOperationUtils.cs
70 lines (60 loc) · 2.23 KB
/
FileOperationUtils.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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using System.Diagnostics;
using System.IO;
using System.Linq;
using Microsoft.Dnx.Runtime;
using Microsoft.Extensions.PlatformAbstractions;
namespace Microsoft.Dnx.Tooling
{
public static class FileOperationUtils
{
public static void DeleteFolder(string folderPath)
{
// Calling DeleteRecursive rather than Directory.Delete(..., recursive: true)
// due to an infrequent exception which can be thrown from that API
DeleteRecursive(folderPath);
}
private static void DeleteRecursive(string deletePath)
{
if (!Directory.Exists(deletePath))
{
return;
}
foreach (var deleteFilePath in Directory.EnumerateFiles(deletePath).Select(Path.GetFileName))
{
File.Delete(Path.Combine(deletePath, deleteFilePath));
}
foreach (var deleteFolderPath in Directory.EnumerateDirectories(deletePath).Select(Path.GetFileName))
{
DeleteRecursive(Path.Combine(deletePath, deleteFolderPath));
Directory.Delete(Path.Combine(deletePath, deleteFolderPath), recursive: true);
}
}
public static bool MarkExecutable(string file)
{
if (RuntimeEnvironmentHelper.IsWindows)
{
return false;
}
var processStartInfo = new ProcessStartInfo()
{
UseShellExecute = false,
FileName = "chmod",
Arguments = string.Format("+x \"{0}\"", file)
};
var process = Process.Start(processStartInfo);
process.WaitForExit();
return process.ExitCode == 0;
}
public static void MakeWritable(string filePath)
{
var attributes = File.GetAttributes(filePath);
if (attributes.HasFlag(FileAttributes.ReadOnly))
{
attributes &= ~FileAttributes.ReadOnly;
File.SetAttributes(filePath, attributes);
}
}
}
}