Subversion Repositories Code-Repo

Rev

Rev 50 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 50 Rev 53
Line 4... Line 4...
4
using System.Text;
4
using System.Text;
5
using System.Diagnostics;
5
using System.Diagnostics;
6
using System.IO;
6
using System.IO;
7
using System.Windows.Forms;
7
using System.Windows.Forms;
8
using System.Security.AccessControl;
8
using System.Security.AccessControl;
-
 
9
using System.DirectoryServices.AccountManagement;
9
 
10
 
10
namespace SWAT_Office_App
11
namespace SWAT_Office_App
11
{
12
{
12
    class ManageUserAccounts
13
    class ManageUserAccounts
13
    {
14
    {
-
 
15
        private static string studentGroupName = "StudentAccounts";
14
        public static List<string> UserAccountsList = new List<string>();
16
        public static List<UserPrincipal> UserAccountsList = new List<UserPrincipal>();
15
        public static bool AddUser(string username, string password)
17
        public static bool AddUser(string username, string password)
16
        {
18
        {
17
            try
19
            try
18
            {
20
            {
-
 
21
                PrincipalContext localSystem = new PrincipalContext(ContextType.Machine);
19
                // Returns true if user was added successfully
22
                // Creates a new user after checking if it already exists or not
20
                foreach (string user in UserAccountsList)
23
                UserPrincipal newUser = new UserPrincipal(localSystem);
-
 
24
                newUser.Name = username;
-
 
25
                PrincipalSearcher searcher = new PrincipalSearcher(newUser);
-
 
26
                Principal result = searcher.FindOne();
-
 
27
                if (result == null)
21
                {
28
                {
-
 
29
                    newUser = new UserPrincipal(localSystem);
22
                    if (username.ToLower() == user.ToLower())
30
                    newUser.Name = username;
-
 
31
                    newUser.SetPassword(password);
-
 
32
                    newUser.PasswordNeverExpires = true;
-
 
33
                    newUser.PasswordNotRequired = false;
-
 
34
                    newUser.UserCannotChangePassword = true;
-
 
35
                    newUser.Description = DateTime.Now.ToShortDateString();
-
 
36
                    newUser.Save();
-
 
37
 
-
 
38
                    // Creates a new group after checking if it already exists or not
-
 
39
                    GroupPrincipal studentGroup = new GroupPrincipal(localSystem);
-
 
40
                    studentGroup.Name = studentGroupName;
-
 
41
                    searcher = new PrincipalSearcher(studentGroup);
-
 
42
                    result = searcher.FindOne();
-
 
43
                    // Creates group if it doesnt already exist
-
 
44
                    if (result == null)
-
 
45
                    {
-
 
46
                        studentGroup = new GroupPrincipal(localSystem, studentGroupName);
-
 
47
                        studentGroup.Name = studentGroupName;
-
 
48
                        studentGroup.IsSecurityGroup = true;
-
 
49
                        studentGroup.Members.Add(newUser);
-
 
50
                        studentGroup.Save();
-
 
51
                    }
-
 
52
                    // Otherwise add to existing group
-
 
53
                    else
23
                    {
54
                    {
-
 
55
                        studentGroup = (GroupPrincipal)result;
24
                        MessageBox.Show("Username already exists", "Error");
56
                        studentGroup.Members.Add(newUser);
25
                        return false;
57
                        studentGroup.Save();
26
                    }
58
                    }
-
 
59
                    DebugText.appendText("Account " + username + " has been created");
-
 
60
                    return true;
-
 
61
                }
-
 
62
                else
-
 
63
                {
-
 
64
                    // local account already exists, return with error
-
 
65
                    return false;
27
                }
66
                }
28
                Process netProcess = new Process();
-
 
29
                netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
-
 
30
                netProcess.StartInfo.FileName = "net.exe";
-
 
31
                netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
-
 
32
                netProcess.StartInfo.CreateNoWindow = true;
-
 
33
                // Arguments for user account creation. Run NET HELP USER from the command prompt for more info.
-
 
34
                netProcess.StartInfo.Arguments = "USER \"" + username + "\" \"" + password + 
-
 
35
                    "\" /ADD /ACTIVE:YES /PASSWORDCHG:NO /PASSWORDREQ:YES /EXPIRES:NEVER /COMMENT:" + DateTime.Now.ToShortDateString();
-
 
36
                netProcess.Start();
-
 
37
                netProcess.WaitForExit();
-
 
38
                netProcess.Close();
-
 
39
                return true;
-
 
40
            }
67
            }
41
            catch (Exception e)
68
            catch (Exception e)
42
            {
69
            {
43
                MessageBox.Show(e.ToString(), "Error");
70
                //MessageBox.Show(e.ToString(), "Error");
-
 
71
                DebugText.appendText(e.ToString());
-
 
72
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
44
                return false;
73
                return false;
45
            }
74
            }
46
        }
75
        }
47
        public static void DeleteUser(List<string> usernames)
76
        public static bool DeleteUser(List<string> usernames)
48
        {
77
        {
49
            try
78
            try
50
            {
79
            {
-
 
80
                bool Success = true;
-
 
81
                PrincipalContext localSystem = new PrincipalContext(ContextType.Machine);
-
 
82
                // Iterates through and deletes selected users
51
                foreach (string user in usernames)
83
                foreach (string user in usernames)
52
                {
84
                {
-
 
85
                    bool deleteShare = false;
53
                    if (Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + user))
86
                    if (Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + user))
54
                    {
87
                    {
55
                        // Prompts for deletion of folder as well as the user account
88
                        // Prompts for deletion of folder as well as the user account
56
                        DialogResult result = MessageBox.Show("A shared folder exists for the user " + user + "!" +
89
                        DialogResult result = MessageBox.Show("A shared folder exists for the user " + user + "!" +
57
                            "\nDelete the folder and all data within the folder?",
90
                            "\nDelete the folder and all data within the folder?",
58
                           "Warning!", MessageBoxButtons.YesNo);
91
                           "Warning!", MessageBoxButtons.YesNo);
59
                        if (result == DialogResult.Yes)
92
                        if (result == DialogResult.Yes)
60
                        {
-
 
61
                            Process netProcess = new Process();
-
 
62
                            netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
-
 
63
                            netProcess.StartInfo.FileName = "net.exe";
-
 
64
                            netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
-
 
65
                            netProcess.StartInfo.CreateNoWindow = true;
-
 
66
                            // Arguments for user account deletion. Run NET HELP USER from the command prompt for more info.
-
 
67
                            netProcess.StartInfo.Arguments = "USER \"" + user + "\" /DELETE";
-
 
68
                            netProcess.Start();
-
 
69
                            netProcess.WaitForExit();
-
 
70
                            netProcess.Close();
-
 
71
 
-
 
72
                            DeleteShareFolder(user);
93
                            deleteShare = true;
73
                        }
-
 
74
                    }
-
 
75
                    // If no folders exist for the user, account is deleted without prompting
-
 
76
                    else
-
 
77
                    {
-
 
78
                        Process netProcess = new Process();
-
 
79
                        netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
-
 
80
                        netProcess.StartInfo.FileName = "net.exe";
-
 
81
                        netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
-
 
82
                        netProcess.StartInfo.CreateNoWindow = true;
-
 
83
                        // Arguments for user account deletion. Run NET HELP USER from the command prompt for more info.
-
 
84
                        netProcess.StartInfo.Arguments = "USER \"" + user + "\" /DELETE";
-
 
85
                        netProcess.Start();
-
 
86
                        netProcess.WaitForExit();
-
 
87
                        netProcess.Close();
-
 
88
                    }
94
                    }
-
 
95
                    // Finds and deletes user and share
-
 
96
                    UserPrincipal toDelete = new UserPrincipal(localSystem);
-
 
97
                    toDelete.Name = user;
-
 
98
                    PrincipalSearcher searcher = new PrincipalSearcher(toDelete);
-
 
99
                    Principal found = searcher.FindOne();
-
 
100
                    toDelete = (UserPrincipal)found;
89
 
-
 
-
 
101
                    toDelete.Delete();
-
 
102
                    DebugText.appendText("Account " + user + " has been deleted");
-
 
103
                    // Deletes share if selected
-
 
104
                    if (deleteShare)
-
 
105
                        if (!DeleteShareFolder(user))
-
 
106
                            Success = false;
90
                }
107
                }
-
 
108
                return Success;
91
            }
109
            }
92
            catch (Exception e)
110
            catch (Exception e)
93
            {
111
            {
94
                MessageBox.Show(e.ToString(), "Error");
112
                //MessageBox.Show(e.ToString(), "Error");
-
 
113
                DebugText.appendText(e.ToString());
-
 
114
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
-
 
115
                return false;
95
            }
116
            }
96
        }
117
        }
97
        public static void QueryUserAccounts()
118
        public static bool QueryUserAccounts()
98
        {
119
        {
99
            // Function that reads the user accounts on the local computer to UserAccountsList
120
            // Function that reads the user accounts on the local computer to UserAccountsList
100
            UserAccountsList.Clear();
121
            UserAccountsList.Clear();
101
            try
122
            try
102
            {
123
            {
103
                Process netProcess = new Process();
124
                PrincipalContext localSystem = new PrincipalContext(ContextType.Machine);
104
                netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
-
 
105
                netProcess.StartInfo.FileName = "net.exe";
-
 
106
                netProcess.StartInfo.UseShellExecute = false;
125
                UserPrincipal user = new UserPrincipal(localSystem);
107
                netProcess.StartInfo.RedirectStandardOutput = true;
-
 
108
                netProcess.StartInfo.CreateNoWindow = true;
-
 
109
                netProcess.StartInfo.Arguments = "USER";
-
 
110
                netProcess.Start();
126
                user.Name = "*";
111
                string netOutput = netProcess.StandardOutput.ReadToEnd();
-
 
112
                // Splits the output into seperate strings for further processing
-
 
113
                string[] tempSplit = netOutput.Split(new string[] { "  ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
-
 
114
                // Roundabout way of trimming each string in the array
-
 
115
                string[] netOutputSplit = new string[tempSplit.Length];
127
                PrincipalSearcher searcher = new PrincipalSearcher(user);
116
                for (int i = 0; i < tempSplit.Length; i++)
-
 
117
                    netOutputSplit[i] = tempSplit[i].Trim();
-
 
118
                // Imports the string array into the UserAccountsList List
128
                PrincipalSearchResult<Principal> result = searcher.FindAll();
119
                UserAccountsList.AddRange(netOutputSplit);
-
 
120
                // Removes the header and footer from the List
-
 
121
                UserAccountsList.RemoveRange(0, 2);
-
 
122
                UserAccountsList.RemoveRange(UserAccountsList.Count() - 1, 1);
-
 
123
                // Removes any additional empty entries
129
                foreach (Principal p in result)
124
                UserAccountsList.RemoveAll(IsStringBlank);
130
                    UserAccountsList.Add((UserPrincipal)p);
125
                netProcess.WaitForExit();
-
 
126
                netProcess.Close();
-
 
127
            }
-
 
128
            catch (Exception e)
-
 
129
            {
-
 
130
                MessageBox.Show(e.ToString(), "Error");
-
 
131
            }
-
 
132
        }
-
 
133
        public static string [] QueryUserAccountExtraInformation(string username)
-
 
134
        {
-
 
135
            // Returns a string array with password requirement and date of account creation
-
 
136
            string[] stringArray = { "", "" };
-
 
137
            try
-
 
138
            {
-
 
139
                Process netProcess = new Process();
-
 
140
                netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
-
 
141
                netProcess.StartInfo.FileName = "net.exe";
-
 
142
                netProcess.StartInfo.UseShellExecute = false;
-
 
143
                netProcess.StartInfo.RedirectStandardOutput = true;
-
 
144
                netProcess.StartInfo.CreateNoWindow = true;
-
 
145
                netProcess.StartInfo.Arguments = "USER " + "\"" + username + "\"";
-
 
146
                netProcess.Start();
131
                return true;
147
                string netOutput = netProcess.StandardOutput.ReadToEnd();
-
 
148
                // Splits the output into seperate strings for further processing
-
 
149
                string[] tempSplit = netOutput.Split(new string[] { "  ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
-
 
150
                // Roundabout way of trimming each string in the array
-
 
151
                string[] netOutputSplit = new string[tempSplit.Length];
-
 
152
                for (int i = 0; i < tempSplit.Length; i++)
-
 
153
                    netOutputSplit[i] = tempSplit[i].Trim();
-
 
154
                stringArray[0] = netOutputSplit[20];
-
 
155
                stringArray[1] = netOutputSplit[4];
-
 
156
                netProcess.WaitForExit();
-
 
157
                netProcess.Close();
-
 
158
            }
132
            }
159
            catch (Exception e)
133
            catch (Exception e)
160
            {
134
            {
161
                MessageBox.Show(e.ToString(), "Error");
135
                //MessageBox.Show(e.ToString(), "Error");
-
 
136
                DebugText.appendText(e.ToString());
-
 
137
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
-
 
138
                return false;
162
            }
139
            }
163
            return stringArray;
-
 
164
        }
140
        }
165
        public static bool QueryUserSharedFolderExist(string username)
141
        public static bool QueryUserSharedFolderExist(string username)
166
        {
142
        {
167
            return Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + username);
143
            return Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + username);
168
        }
144
        }
169
        public static void CreateShareFolder(string username)
145
        public static bool CreateShareFolder(string username)
170
        {
146
        {
171
            
147
            
172
            try
148
            try
173
            {
149
            {
174
                // Creates the directory with only the specific NTFS and share permissions for the user.
150
                // Creates the directory with only the specific NTFS and share permissions for the user.
175
                DirectorySecurity dSecurity = new DirectorySecurity();
151
                DirectorySecurity dSecurity = new DirectorySecurity();
-
 
152
                // Adds NTFS permissions for system accounts
176
                foreach (string systemUser in Settings_Form.systemAccounts)
153
                foreach (string systemUser in Settings_Form.systemAccounts)
177
                {
154
                {
178
                    dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + systemUser, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
155
                    dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + systemUser, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
179
                    dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + systemUser, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
156
                    dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + systemUser, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
180
                }
157
                }
181
                //dSecurity.AddAccessRule(new FileSystemAccessRule("\\Administrator", FileSystemRights.FullControl, AccessControlType.Allow));
-
 
182
                //dSecurity.AddAccessRule(new FileSystemAccessRule("\\SWAT", FileSystemRights.FullControl, AccessControlType.Allow));
158
                // Adds NTFS permissions for the user
183
                //dSecurity.AddAccessRule(new FileSystemAccessRule("\\Administrators", FileSystemRights.FullControl, AccessControlType.Allow));
-
 
184
                dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
159
                dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ContainerInherit, PropagationFlags.None, AccessControlType.Allow));
185
                dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
160
                dSecurity.AddAccessRule(new FileSystemAccessRule("\\" + username, FileSystemRights.FullControl, InheritanceFlags.ObjectInherit, PropagationFlags.None, AccessControlType.Allow));
186
                Directory.CreateDirectory(Settings_Form.sharedFolderLocation + @"\" + username, dSecurity);
161
                Directory.CreateDirectory(Settings_Form.sharedFolderLocation + @"\" + username, dSecurity);
-
 
162
                DebugText.appendText("Shared folder for " + username + " has been created");
187
                SetSharePermissions(username);
163
                return SetSharePermissions(username);
188
            }
164
            }
189
            catch (Exception e)
165
            catch (Exception e)
190
            {
166
            {
191
                MessageBox.Show(e.ToString(), "Error");
167
                //MessageBox.Show(e.ToString(), "Error");
-
 
168
                DebugText.appendText(e.ToString());
-
 
169
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
-
 
170
                return false;
192
            }
171
            }
193
        }
172
        }
194
        public static void DeleteShareFolder(string username)
173
        public static bool DeleteShareFolder(string username)
195
        {
174
        {
196
            try
175
            try
197
            {
176
            {
-
 
177
                bool Success = true;
198
                // Removes the share BEFORE deleting the folder. Otherwise share will error on remove.
178
                // Removes the share BEFORE deleting the folder. Otherwise share will error on remove.
199
                RemoveSharePermissions(username);
179
                if (!RemoveSharePermissions(username))
-
 
180
                    Success = false;
200
                Directory.Delete(Settings_Form.sharedFolderLocation + @"\" + username, true);
181
                Directory.Delete(Settings_Form.sharedFolderLocation + @"\" + username, true);
-
 
182
                DebugText.appendText("Shared folder for " + username + " has been deleted");
-
 
183
                return Success;
201
            }
184
            }
202
            catch (Exception e)
185
            catch (Exception e)
203
            {
186
            {
204
                MessageBox.Show(e.ToString(), "Error");
187
                //MessageBox.Show(e.ToString(), "Error");
-
 
188
                DebugText.appendText(e.ToString());
-
 
189
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
-
 
190
                return false;
205
            }
191
            }
206
        }
192
        }
207
        public static void ToggleShare(string username)
193
        public static bool ToggleShare(string username)
208
        {
194
        {
209
            try
195
            try
210
            {
196
            {
-
 
197
                // Checks if a share already exists for the user
211
                if (Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + username))
198
                if (Directory.Exists(Settings_Form.sharedFolderLocation + @"\" + username))
212
                {
199
                {
213
                    DialogResult result = MessageBox.Show("A shared folder exists for the user " + username + "!" +
200
                    DialogResult result = MessageBox.Show("A shared folder exists for the user " + username + "!" +
214
                                "\nDelete the folder and all data within the folder?",
201
                                "\nDelete the folder and all data within the folder?",
215
                               "Warning!", MessageBoxButtons.YesNo);
202
                               "Warning!", MessageBoxButtons.YesNo);
216
                    if (result == DialogResult.Yes)
203
                    if (result == DialogResult.Yes)
217
                    {
-
 
218
                        DeleteShareFolder(username);
204
                        return DeleteShareFolder(username);
219
                    }
-
 
220
                }
205
                }
-
 
206
                // Otherwise creates the share for the user
221
                else
207
                else
222
                {
-
 
223
                    CreateShareFolder(username);
208
                    return CreateShareFolder(username);
224
                }
209
                return true;
225
            }
210
            }
226
            catch (Exception e)
211
            catch (Exception e)
227
            {
212
            {
228
                MessageBox.Show(e.ToString(), "Error");
213
                //MessageBox.Show(e.ToString(), "Error");
-
 
214
                DebugText.appendText(e.ToString());
-
 
215
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
-
 
216
                return false;
229
            }
217
            }
230
        }
218
        }
231
        public static long GetShareSize(string username)
219
        public static long GetShareSize(string username)
232
        {
220
        {
233
            DirectoryInfo dir = new DirectoryInfo(Settings_Form.sharedFolderLocation + @"\" + username);
221
            DirectoryInfo dir = new DirectoryInfo(Settings_Form.sharedFolderLocation + @"\" + username);
234
            long size = GetDirSize(dir);
222
            long size = GetDirSize(dir);
235
            return size;
223
            return size;
236
        }
224
        }
237
        public static void ChangeUserPassword(string username, string password)
225
        private static long GetDirSize(DirectoryInfo input)
238
        {
226
        {
239
            try
227
            try
240
            {
228
            {
-
 
229
                if (input.Exists)
-
 
230
                {
-
 
231
                    long size = 0;
241
                Process netProcess = new Process();
232
                    FileInfo[] files = input.GetFiles();
-
 
233
                    foreach (FileInfo file in files)
-
 
234
                    {
-
 
235
                        size += file.Length;
-
 
236
                    }
242
                netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
237
                    DirectoryInfo[] dirs = input.GetDirectories();
243
                netProcess.StartInfo.FileName = "net.exe";
238
                    foreach (DirectoryInfo dir in dirs)
-
 
239
                    {
-
 
240
                        size += GetDirSize(dir);
-
 
241
                    }
-
 
242
                    return size;
-
 
243
                }
-
 
244
                else 
-
 
245
                    return 0;
-
 
246
            }
-
 
247
            catch (Exception e)
-
 
248
            {
244
                netProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
249
                //MessageBox.Show(e.ToString(), "Error");
245
                netProcess.StartInfo.CreateNoWindow = true;
250
                DebugText.appendText(e.ToString());
246
                // Arguments for changing user password. Run NET HELP USER from the command prompt for more info.
251
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
-
 
252
                return 0;
-
 
253
            }
-
 
254
        }
247
                netProcess.StartInfo.Arguments = "USER \"" + username + "\" \"" + password + "\"";
255
        public static bool ChangeUserPassword(string username, string password)
-
 
256
        {
-
 
257
            try
-
 
258
            {
-
 
259
                // Queries for the specified user password
-
 
260
                PrincipalContext localSystem = new PrincipalContext(ContextType.Machine);
-
 
261
                UserPrincipal user = new UserPrincipal(localSystem);
248
                netProcess.Start();
262
                user.Name = username;
-
 
263
                PrincipalSearcher searcher = new PrincipalSearcher(user);
-
 
264
                Principal result = searcher.FindOne();
-
 
265
                user = (UserPrincipal)result;
249
                netProcess.WaitForExit();
266
                user.SetPassword(password);
250
                netProcess.Close();
267
                user.Save();
-
 
268
                DebugText.appendText("Password for " + username + " has been changed");
-
 
269
                return true;
251
            }
270
            }
252
            catch (Exception e)
271
            catch (Exception e)
253
            {
272
            {
254
                MessageBox.Show(e.ToString(), "Error");
273
                //MessageBox.Show(e.ToString(), "Error");
-
 
274
                DebugText.appendText(e.ToString());
-
 
275
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
-
 
276
                return false;
255
            }
277
            }
256
        }
278
        }
257
        public static bool SessionsOpen()
279
        public static bool SessionsOpen()
258
        {
280
        {
259
            bool sessionsOpen = true;
-
 
260
            try
281
            try
261
            {
282
            {
262
                Process netProcess = new Process();
283
                Process netProcess = new Process();
263
                netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
284
                netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
264
                netProcess.StartInfo.FileName = "net.exe";
285
                netProcess.StartInfo.FileName = "net.exe";
Line 272... Line 293...
272
                string[] tempSplit = netOutput.Split(new string[] { "  ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
293
                string[] tempSplit = netOutput.Split(new string[] { "  ", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries);
273
                netProcess.WaitForExit();
294
                netProcess.WaitForExit();
274
                netProcess.Close();
295
                netProcess.Close();
275
 
296
 
276
                if (tempSplit[0] == "There are no entries in the list.")
297
                if (tempSplit[0] == "There are no entries in the list.")
277
                    sessionsOpen = false;
-
 
278
            }
-
 
279
            catch (Exception e)
-
 
280
            {
-
 
281
                MessageBox.Show(e.ToString(), "Error");
-
 
282
            }
-
 
283
            return sessionsOpen;
-
 
284
        }
-
 
285
        private static long GetDirSize(DirectoryInfo entry)
-
 
286
        {
-
 
287
            try
-
 
288
            {
-
 
289
                if (entry.Exists)
-
 
290
                {
-
 
291
                    long size = 0;
-
 
292
                    FileInfo[] files = entry.GetFiles();
-
 
293
                    foreach (FileInfo file in files)
-
 
294
                    {
-
 
295
                        size += file.Length;
-
 
296
                    }
-
 
297
                    DirectoryInfo[] dirs = entry.GetDirectories();
-
 
298
                    foreach (DirectoryInfo dir in dirs)
-
 
299
                    {
-
 
300
                        size += GetDirSize(dir);
-
 
301
                    }
-
 
302
                    return size;
298
                    return false;
303
                }
-
 
304
                else
299
                else
305
                    return 0;
300
                    return true;
306
            }
301
            }
307
            catch (Exception e)
302
            catch (Exception e)
308
            {
303
            {
309
                MessageBox.Show(e.ToString(), "Error");
304
                //MessageBox.Show(e.ToString(), "Error");
-
 
305
                DebugText.appendText(e.ToString());
-
 
306
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
310
                return 0;
307
                return true;
311
            }
308
            }
312
        }
309
        }
313
        private static void SetSharePermissions(string username)
310
        private static bool SetSharePermissions(string username)
314
        {
311
        {
315
            string combinationString = "";
312
            string combinationString = "";
316
            foreach (string systemUser in Settings_Form.systemAccounts)
313
            foreach (string systemUser in Settings_Form.systemAccounts)
317
            {
314
            {
318
                combinationString = combinationString.Insert(combinationString.Length, " /GRANT:\"" + systemUser + "\",FULL");
315
                combinationString = combinationString.Insert(combinationString.Length, " /GRANT:\"" + systemUser + "\",FULL");
Line 327... Line 324...
327
                netProcess.StartInfo.CreateNoWindow = true;
324
                netProcess.StartInfo.CreateNoWindow = true;
328
                netProcess.StartInfo.Arguments = "SHARE \"" + username + "\"=\"" + Settings_Form.sharedFolderLocation + "\\" + username + "\" " + combinationString;
325
                netProcess.StartInfo.Arguments = "SHARE \"" + username + "\"=\"" + Settings_Form.sharedFolderLocation + "\\" + username + "\" " + combinationString;
329
                netProcess.Start();
326
                netProcess.Start();
330
                netProcess.WaitForExit();
327
                netProcess.WaitForExit();
331
                netProcess.Close();
328
                netProcess.Close();
-
 
329
                DebugText.appendText("Share permissions for " + username + " has been set");
-
 
330
                return true;
332
            }
331
            }
333
            catch (Exception e)
332
            catch (Exception e)
334
            {
333
            {
335
                MessageBox.Show(e.ToString(), "Error");
334
                //MessageBox.Show(e.ToString(), "Error");
-
 
335
                DebugText.appendText(e.ToString());
-
 
336
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
-
 
337
                return false;
336
            }
338
            }
337
        }
339
        }
338
        private static void RemoveSharePermissions(string username)
340
        private static bool RemoveSharePermissions(string username)
339
        {
341
        {
340
            try
342
            try
341
            {
343
            {
342
                Process netProcess = new Process();
344
                Process netProcess = new Process();
343
                netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
345
                netProcess.StartInfo.WorkingDirectory = System.Environment.SystemDirectory;
Line 346... Line 348...
346
                netProcess.StartInfo.CreateNoWindow = true;
348
                netProcess.StartInfo.CreateNoWindow = true;
347
                netProcess.StartInfo.Arguments = "SHARE \"" + username + "\" /Delete";
349
                netProcess.StartInfo.Arguments = "SHARE \"" + username + "\" /Delete";
348
                netProcess.Start();
350
                netProcess.Start();
349
                netProcess.WaitForExit();
351
                netProcess.WaitForExit();
350
                netProcess.Close();
352
                netProcess.Close();
-
 
353
                DebugText.appendText("Share permissions for " + username + " has been removed");
-
 
354
                return true;
351
            }
355
            }
352
            catch (Exception e)
356
            catch (Exception e)
353
            {
357
            {
354
                MessageBox.Show(e.ToString(), "Error");
358
                //MessageBox.Show(e.ToString(), "Error");
355
            }
-
 
356
        }
-
 
357
        private static bool IsStringBlank(string s)
359
                DebugText.appendText(e.ToString());
358
        {
-
 
359
            if (s == "")
-
 
360
                return true;
360
                MessageBox.Show("An error has occured. Please notify a supervisor to debug.", "Error");
361
            else
-
 
362
                return false;
361
                return false;
-
 
362
            }
363
        }
363
        }
364
    }
364
    }
365
}
365
}