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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Tango.Core.Helpers
{
/// <summary>
/// Contains several path helper methods.
/// </summary>
public static class PathHelper
{
/// <summary>
/// Gets the user tango folder (%appdata%\Twine\Tango).
/// </summary>
/// <returns></returns>
public static String GetUserTangoFolder()
{
return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "Twine", "Tango");
}
/// <summary>
/// Gets the application startup path.
/// </summary>
/// <returns></returns>
public static String GetStartupPath()
{
return AppDomain.CurrentDomain.BaseDirectory;
}
/// <summary>
/// Gets the solution folder.
/// </summary>
/// <returns></returns>
public static String GetSolutionFolder()
{
DirectoryInfo path = new DirectoryInfo(GetStartupPath());
while (path.Name != "Visual_Studio")
{
path = path.Parent;
}
return path.FullName;
}
/// <summary>
/// Tries to delete the specified file.
/// </summary>
/// <param name="path">The file path.</param>
/// <returns></returns>
public static bool TryDeleteFile(String path)
{
try
{
File.Delete(path);
return true;
}
catch
{
return false;
}
}
/// <summary>
/// Copies the specified directory source path to the specified destination.
/// </summary>
/// <param name="sourcePath">The source path.</param>
/// <param name="destinationPath">The destination path.</param>
/// <param name="copySubDirs">if set to <c>true</c> will copy sub directories.</param>
/// <exception cref="DirectoryNotFoundException">Source directory does not exist or could not be found: "
/// + sourcePath</exception>
public static void CopyDirectory(string sourcePath, string destinationPath, bool copySubDirs, Action<int, int> progress = null)
{
CopyDirectoryInternal(sourcePath, destinationPath, copySubDirs, progress, Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories).Length, 0);
}
private static void CopyDirectoryInternal(string sourcePath, string destinationPath, bool copySubDirs, Action<int, int> progress, int total, int current)
{
// Get the subdirectories for the specified directory.
DirectoryInfo dir = new DirectoryInfo(sourcePath);
if (!dir.Exists)
{
throw new DirectoryNotFoundException(
"Source directory does not exist or could not be found: "
+ sourcePath);
}
DirectoryInfo[] dirs = dir.GetDirectories();
// If the destination directory doesn't exist, create it.
if (!Directory.Exists(destinationPath))
{
Directory.CreateDirectory(destinationPath);
}
// Get the files in the directory and copy them to the new location.
FileInfo[] files = dir.GetFiles();
foreach (FileInfo file in files)
{
string temppath = Path.Combine(destinationPath, file.Name);
file.CopyTo(temppath, false);
current++;
progress?.Invoke(current, total);
}
// If copying subdirectories, copy them and their contents to new location.
if (copySubDirs)
{
foreach (DirectoryInfo subdir in dirs)
{
string temppath = Path.Combine(destinationPath, subdir.Name);
CopyDirectoryInternal(subdir.FullName, temppath, copySubDirs, progress, total, current);
}
}
}
}
}
|