This repository has been archived by the owner on Nov 26, 2017. It is now read-only.
/
Globals.cs
152 lines (134 loc) · 4.28 KB
/
Globals.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
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
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlServerCe;
using System.Diagnostics;
using System.IO;
//using System.Linq;
//using System.Security.Policy;
//using System.Security.Principal;
//using System.Security.AccessControl;
using System.Security;
using System.Security.Permissions;
//using System.Text;
//using System.Windows.Forms;
namespace Switchex {
public class Globals {
public static frmError frmError = new frmError();
public static List<Profile> profiles = new List<Profile>();
public enum FileAction {
CreateFolder,
DeleteFolder,
CreateFile,
WriteFile,
CopyFile,
DeleteFile,
CreateBoth
};
public static bool blnUpdates = false,
resetSettings = false;
public static int rowCount = 0;
public static string
downloadVersion = null,
selectedID = null,
selectedName = null,
selectedIP = null,
selectedWebsite = null,
selectedPatch = null,
selectedRating = null,
selectedNotes = null,
selectedProfile = null,
currentProfile = null,
changelogFile = System.Windows.Forms.Application.StartupPath + "\\Resources\\changelog.txt",
helpFile = System.Windows.Forms.Application.StartupPath + "\\Resources\\Switchex Help.html";
public static int RunActionsExecutable(FileAction action, string[] args) {
Process p = new Process();
p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
p.StartInfo.FileName = "fileactions.exe";
p.StartInfo.UseShellExecute = true;
try {
switch(action) {
case FileAction.WriteFile:
// args: change "path\to\file" "text to write"
// note: can only be a text file (any extension)
p.StartInfo.Arguments = "write \"" + args[0] + "\" \"\"" + args[1] + "\"\"";
break;
case FileAction.CreateBoth:
// args: create both "path\to\folder" "path\to\file" ["text to write"]
p.StartInfo.Arguments = "create both \"" + args[0] + "\" \"" + args[1] + "\"";
if(args.Length > 2 && args[2] != null) {
p.StartInfo.Arguments += " \"\"" + args[2] + "\"\"";
}
break;
case FileAction.CreateFile:
// args: create file "path\to\file" ["text to write"]
// note: ["text to write"] is for text files (any extension)
p.StartInfo.Arguments = "create file \"" + args[0] + "\"";
if(args.Length > 1 && args[1] != null) {
p.StartInfo.Arguments += " \"\"" + args[1] + "\"\"";
}
break;
case FileAction.CreateFolder:
// args: create folder "path\to\create"
p.StartInfo.Arguments = "create folder \"" + args[0] + "\"";
break;
case FileAction.DeleteFile:
// args: delete file "path\to\file"
p.StartInfo.Arguments = "delete file \"" + args[0] + "\"";
break;
case FileAction.DeleteFolder:
// args: delete folder "path\to\delete"
p.StartInfo.Arguments = "delete folder \"" + args[0] + "\"";
break;
case FileAction.CopyFile:
// args: copy "true/false" (delete source file) "file\to\copy" "path\to\new\location"
p.StartInfo.Arguments = "copy " + args[0] + " \"" + args[1] + "\" \"" + args[2] + "\"";
break;
}
p.Start();
p.WaitForExit();
if(p.ExitCode == 1) {
p.StartInfo.Verb = "runas";
p.Start();
p.WaitForExit();
if(p.ExitCode != 0) {
return -1;
}
}
return p.ExitCode;
}
catch(Exception ex) {
if(ex.Message.Contains("canceled by the user")) {
return 5;
}
return -1;
}
}
/// <summary>
/// Get all profiles and add them to a global list.
/// </summary>
public static void GetProfiles() {
profiles.Clear();
try {
using(SqlCeConnection conn = new SqlCeConnection(Properties.Settings.Default.switchexConnectionString)) {
using(SqlCeCommand cmd = new SqlCeCommand("SELECT * FROM Profiles", conn)) {
using(DataSet ds = new DataSet()) {
using(SqlCeDataAdapter adapter = new SqlCeDataAdapter(cmd)) {
adapter.SelectCommand = cmd;
adapter.Fill(ds);
foreach(DataRow row in ds.Tables[0].Rows) {
profiles.Add(new Profile(false, Convert.ToInt32(row.ItemArray[0])));
}
Globals.profiles.Sort((x, y) => x.ProfileName.CompareTo(y.ProfileName));
}
}
}
}
}
catch(Exception ex) {
Globals.frmError.ShowDialog(ex);
}
}
}
}