-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtils.cs
More file actions
166 lines (150 loc) · 6.2 KB
/
Utils.cs
File metadata and controls
166 lines (150 loc) · 6.2 KB
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
namespace RSNiniManager
{
class Utils
{
public static void CreateBuckup(List<string> RevitServesFolders, string autodeskDataFolder, string fullBackupFolderPath)
{
if (!Directory.Exists(fullBackupFolderPath))
{
Directory.CreateDirectory(fullBackupFolderPath);
foreach (var autodeskfolder in RevitServesFolders)
{
CopyDirectory(autodeskfolder, autodeskfolder.Replace(autodeskDataFolder, fullBackupFolderPath), true);
}
}
else
{
foreach (var autodeskfolder in RevitServesFolders)
{
HashSet<string> servers = new HashSet<string>();
string backupFolder = autodeskfolder.Replace(autodeskDataFolder, fullBackupFolderPath);
GetServersFromini(backupFolder).ForEach(server => servers.Add(server));
GetServersFromini(autodeskfolder).ForEach(server => servers.Add(server));
SetServersIni(backupFolder, servers.ToList());
}
}
}
public static void GetServersFromBackup(List<string> RevitServesFolders,
string autodeskDataFolder,
string fullBackupFolderPath,
ref Dictionary<string, List<ServerObject>> RevitsAndServers)
{
foreach (var autodeskFolder in RevitServesFolders)
{
string backupFolder = autodeskFolder.Replace(autodeskDataFolder, fullBackupFolderPath);
List<ServerObject> backupini = GetServersFromini(backupFolder, false);
string folderKey = autodeskFolder.Replace(autodeskDataFolder, "");
if (RevitsAndServers.ContainsKey(folderKey))
{
foreach (ServerObject server in backupini)
{
if (!RevitsAndServers[folderKey].Contains(server))
{
RevitsAndServers[folderKey].Add(server);
}
}
}
else
{
RevitsAndServers[folderKey] = backupini;
}
}
}
public static List<ServerObject> GetServersFromini(string folder, bool marked)
{
var rsnFilePath = Path.Combine(folder, "Config", "RSN.ini");
var ipServerList = new List<ServerObject>();
if (File.Exists(rsnFilePath))
{
var ipList = File.ReadAllLines(rsnFilePath).ToList();
ipList.ForEach(serv => ipServerList.Add(new ServerObject(serv, marked)));
}
return ipServerList;
}
public static List<string> GetServersFromini(string folder)
{
var rsnFilePath = Path.Combine(folder, "Config", "RSN.ini");
var ipServerList = new List<string>();
if (File.Exists(rsnFilePath))
{
ipServerList = File.ReadAllLines(rsnFilePath).ToList();
}
return ipServerList;
}
public static void SetServersIni(string folder, List<string> newServers)
{
var rsnFilePath = Path.Combine(folder, "Config", "RSN.ini");
if (File.Exists(rsnFilePath))
{
File.WriteAllLines(rsnFilePath, newServers);
}
}
public static void CopyDirectory(string sourceDir, string destinationDir, bool recursive)
{
// Get information about the source directory
var dir = new DirectoryInfo(sourceDir);
// Check if the source directory exists
if (!dir.Exists)
throw new DirectoryNotFoundException($"Source directory not found: {dir.FullName}");
// Cache directories before we start copying
DirectoryInfo[] dirs = dir.GetDirectories();
// Create the destination directory
Directory.CreateDirectory(destinationDir);
// Get the files in the source directory and copy to the destination directory
foreach (FileInfo file in dir.GetFiles())
{
string targetFilePath = Path.Combine(destinationDir, file.Name);
file.CopyTo(targetFilePath);
}
// If recursive and copying subdirectories, recursively call this method
if (recursive)
{
foreach (DirectoryInfo subDir in dirs)
{
string newDestinationDir = Path.Combine(destinationDir, subDir.Name);
CopyDirectory(subDir.FullName, newDestinationDir, true);
}
}
}
public static string ReadLineOrEsc()
{
string retString = "";
int curIndex = 0;
while (true)
{
ConsoleKeyInfo readKeyResult = Console.ReadKey(true);
// handle Esc
if (readKeyResult.Key == ConsoleKey.Escape)
{
Console.WriteLine();
return null;
}
// handle Enter
if (readKeyResult.Key == ConsoleKey.Enter)
{
Console.WriteLine();
return retString;
}
// handle backspace
if (readKeyResult.Key == ConsoleKey.Backspace)
{
if (curIndex > 0)
{
retString = retString.Remove(retString.Length - 1);
Console.Write(readKeyResult.KeyChar);
Console.Write(' ');
Console.Write(readKeyResult.KeyChar);
curIndex--;
}
}
else
// handle all other keypresses
{
retString += readKeyResult.KeyChar;
Console.Write(readKeyResult.KeyChar);
curIndex++;
}
}
}
}
}