-
Notifications
You must be signed in to change notification settings - Fork 0
/
Helper.cs
100 lines (94 loc) · 2.96 KB
/
Helper.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
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using Microsoft.WindowsAPICodePack.Dialogs;
namespace uLab_system_builder
{
public class Helper : Window
{
public static void AppendToFile(string fileName, string text)
{
using (System.IO.StreamWriter file = new System.IO.StreamWriter(fileName, true))
{
file.WriteLine(text);
}
}
public static void DeleteFile(string fileName)
{
if (File.Exists(fileName))
{
// 2 lines before delete clear garbage collection so that the file can be unlocked
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
File.Delete(fileName);
}
}
public static string GetCurrentDateAndTime()
{
string dateAndTime = DateTime.Now.ToString("HH:mm:ss", System.Globalization.DateTimeFormatInfo.InvariantInfo);
dateAndTime += " "
+ CultureInfo.CurrentCulture.DateTimeFormat.GetMonthName(DateTime.Now.Month)
+ DateTime.Now.ToString(" d, yyyy");
return dateAndTime;
}
public static bool IsProjectNameValid(string name)
{
if (name.Length < 1)
return false;
if (!IsLetter(name[0]))
return false;
return true;
}
public static bool IsLetter(char letter) // range: A-Z and a-z
{
if ((letter >= 65 && letter <= 90) || (letter >= 97 && letter <= 122))
return true;
return false;
}
public static string GetValidFolderPath()
{
try
{
string path = "";
CommonOpenFileDialog dialog = new CommonOpenFileDialog();
dialog.InitialDirectory = path;
dialog.IsFolderPicker = true;
if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
{
path = dialog.FileName;
}
if (Directory.Exists(path))
{
return path;
}
else
{
return "-1";
}
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
return "-1";
}
}
public static void ErrorMessage(string error)
{
MessageBox.Show(error, "ERROR", MessageBoxButton.OK, MessageBoxImage.Error);
}
}
}