-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileSystem.cs
More file actions
291 lines (248 loc) · 9.95 KB
/
FileSystem.cs
File metadata and controls
291 lines (248 loc) · 9.95 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
using Microsoft.Win32;
using System;
using System.IO;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Windows.Forms;
namespace CardonerSistemas
{
static class FileSystem
{
private static string folderDropbox;
private static string folderGoogleDrive;
private static string folderOneDrive;
#region Path format functions
internal static string PathAddBackslash(string path)
{
if (path == null)
{
throw new ArgumentNullException(nameof(path));
}
path = path.TrimEnd();
if (PathEndsWithDirectorySeparator(path))
{
return path;
}
return path + GetDirectorySeparatorUsedInPath(path);
}
internal static bool PathEndsWithDirectorySeparator(string path)
{
if (path.Length == 0)
{
return false;
}
return path.EndsWith(Path.DirectorySeparatorChar.ToString()) || path.EndsWith(Path.AltDirectorySeparatorChar.ToString());
}
internal static string RemoveDirectorySeparatorAtEnd(string path)
{
if (PathEndsWithDirectorySeparator(path))
{
return RemoveDirectorySeparatorAtEnd(path.Substring(0, path.Length - 1));
}
else
{
return path;
}
}
internal static char GetDirectorySeparatorUsedInPath(string path)
{
if (path.Contains(Path.AltDirectorySeparatorChar.ToString()))
{
return Path.AltDirectorySeparatorChar;
}
return Path.DirectorySeparatorChar;
}
#endregion
#region Process folder name
private const string FolderTagDropbox = "{Dropbox}";
private const string FolderTagGoogleDrive = "{GoogleDrive}";
private const string FolderTagOneDrive = "{OneDrive}";
static internal string ProcessFolderName(string folderName, bool ignoreCase = true)
{
if (System.String.IsNullOrWhiteSpace(folderName))
{
return System.String.Empty;
}
string folderNameProcessed = folderName;
// Case sensitive
string folderNameForContains;
string folderTagDropboxForContains;
string folderTagGoogleDriveForContains;
string folderTagOneDriveForContains;
if (ignoreCase)
{
folderNameForContains = folderName.ToLower();
folderTagDropboxForContains = FolderTagDropbox.ToLower();
folderTagGoogleDriveForContains = FolderTagGoogleDrive.ToLower();
folderTagOneDriveForContains = FolderTagOneDrive.ToLower();
}
else
{
folderNameForContains = folderName;
folderTagDropboxForContains = FolderTagDropbox;
folderTagGoogleDriveForContains = FolderTagGoogleDrive;
folderTagOneDriveForContains = FolderTagOneDrive;
}
// Replace DropBox path
if (folderNameForContains.Contains(folderTagDropboxForContains.ToLower()))
{
string dropboxFolder = string.Empty;
if (GetDropboxPath(ref dropboxFolder))
{
folderNameProcessed = Regex.Replace(folderNameProcessed, FolderTagDropbox, dropboxFolder, RegexOptions.IgnoreCase).Trim();
}
}
// Replace Google Drive path
if (folderNameForContains.Contains(folderTagGoogleDriveForContains))
{
string googleDriveFolder = string.Empty;
if (GetGoogleDrivePath(ref googleDriveFolder))
{
folderNameProcessed = Regex.Replace(folderNameProcessed, FolderTagGoogleDrive, googleDriveFolder, RegexOptions.IgnoreCase).Trim();
}
}
// Replace OneDrive path
if (folderNameForContains.Contains(folderTagOneDriveForContains))
{
string oneDriveFolder = string.Empty;
if (GetOneDrivePath(ref oneDriveFolder))
{
folderNameProcessed = Regex.Replace(folderNameProcessed, FolderTagOneDrive, oneDriveFolder, RegexOptions.IgnoreCase).Trim();
}
}
return folderNameProcessed;
}
#endregion
#region Cloud storage - Dropbox
#pragma warning disable IDE1006 // Naming Styles
#pragma warning disable S3459 // Unassigned members should be removed
#pragma warning disable S1144 // Unused private types or members should be removed
private sealed class DropboxConfigInfoRoot
{
public DropboxConfigInfoPersonal personal { get; set; }
}
private sealed class DropboxConfigInfoPersonal
{
public string path { get; set; }
public long host { get; set; }
public bool is_team { get; set; }
public string subscription_type { get; set; }
}
#pragma warning restore IDE1006 // Naming Styles
#pragma warning restore S3459 // Unassigned members should be removed
#pragma warning restore S1144 // Unused private types or members should be removed
internal static bool GetDropboxPath(ref string path)
{
if (folderDropbox != null)
{
path = folderDropbox;
return true;
}
const string folderName = "Dropbox";
const string configFilename = "info.json";
string applicationDatafolder;
string configFilePath;
string configFileString;
DropboxConfigInfoRoot configInfo;
// Gets the path to the Dropbox config file
applicationDatafolder = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
if (applicationDatafolder.Length != 0)
{
configFilePath = Path.Combine(applicationDatafolder, folderName, configFilename);
if (System.IO.File.Exists(configFilePath))
{
try
{
configFileString = System.IO.File.ReadAllText(configFilePath);
}
catch (Exception ex)
{
MessageBox.Show($"Ha ocurrido un error al leer el archivo de configuración de Dropbox ({configFilePath})\n\nError: {ex.Message}", CardonerSistemas.My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
try
{
configInfo = JsonSerializer.Deserialize<DropboxConfigInfoRoot>(configFileString);
}
catch (Exception ex)
{
MessageBox.Show($"Ha ocurrido un error al interpretar el archivo de configuración de Dropbox ({configFilename})\n\nError: {ex.Message}", CardonerSistemas.My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
if (configInfo.personal != null && configInfo.personal.path != null)
{
path = configInfo.personal.path;
folderDropbox = path;
return true;
}
}
}
return false;
}
#endregion
#region Cloud storage - Google Drive
internal static bool GetGoogleDrivePath(ref string path)
{
if (folderGoogleDrive != null)
{
path = folderGoogleDrive;
return true;
}
try
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Google\\Drive"))
{
if (key != null)
{
Object value = key.GetValue("Path");
if (value != null)
{
path = value.ToString();
folderGoogleDrive = path;
return true;
}
}
}
return false;
}
catch (Exception ex)
{
MessageBox.Show($"Ha ocurrido un error al obtener la ubicación de Google Drive desde el Registro de Windows.\n\nError: {ex.Message}", CardonerSistemas.My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
#endregion
#region Cloud storage - OneDrive
internal static bool GetOneDrivePath(ref string path)
{
if (folderOneDrive != null)
{
path = folderOneDrive;
return true;
}
try
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey("SOFTWARE\\Microsoft\\OneDrive"))
{
if (key != null)
{
Object value = key.GetValue("UserFolder");
if (value != null)
{
path = value.ToString();
folderOneDrive = path;
return true;
}
}
}
return false;
}
catch (Exception ex)
{
MessageBox.Show($"Ha ocurrido un error al obtener la ubicación de OneDrive desde el Registro de Windows.\n\nError: {ex.Message}", CardonerSistemas.My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
return false;
}
}
#endregion
}
}