public static string ShowFolders() { string output = ""; Queue <Client> q = Client.clientqueue; for (int i = 0; i < q.Count; i++) //iterare on all clients and get their list of shared folders { Client temp = q.Dequeue(); string mach = temp.GetMach(); TryCon(mach); SelectQuery sq = new SelectQuery("SELECT * FROM Win32_Share"); ManagementObjectSearcher searcher = new ManagementObjectSearcher(ms, sq); output += mach + "'s shared folders and drives: \n"; foreach (ManagementObject mo in searcher.Get()) { if (!mo["Name"].ToString().Contains("$") && mo["Path"].ToString().Length > 3 && mo["Name"].ToString() != "Users") { output += mo["Path"] + "\n"; } } q.Enqueue(temp); } //add shared folders of the server ManagementObjectSearcher searcher2 = new ManagementObjectSearcher("\\\\" + Environment.MachineName + "\\root\\cimv2", "SELECT * FROM Win32_Share"); //win32_share contains the list of all shared instances (folders, drives...) output += "\n" + Environment.MachineName + "'s shared folders and drives: \n"; foreach (ManagementObject mo in searcher2.Get()) { if (!mo["Name"].ToString().Contains("$") && mo["Name"].ToString().Length > 3 && mo["Name"].ToString() != "Users") { output += mo["Path"] + "\n"; } } return(output); }
private static string[] Interpreter(string command) { try { string[] keywords = { " from ", " on " }; int end = 0, start = 0; string shortened, com, param = " "; char detect = ' '; bool flag = false, found = false; Stack <string> st = new Stack <string>(); for (int i = 0; i < command.Length; i++) // remove double spaces { if (command[i] == ' ' && command[i + 1] == ' ') { command = command.Remove(i + 1, 1); } } foreach (string word in keywords) // search for pc name { if (command.IndexOf(word) != -1) { start = command.IndexOf(word) + word.Length; shortened = command.Substring(start); param = shortened.Substring(0); flag = true; break; } } if (flag) { for (int i = 0; i < Client.clientqueue.Count; i++) { Client c = Client.clientqueue.Dequeue(); if (c.GetMach() == param || c.GetNick() == param) { st.Push(c.GetMach()); Client.clientqueue.Enqueue(c); flag = false; break; } Client.clientqueue.Enqueue(c); } if (flag) { st.Push(Environment.MachineName); } } else { st.Push(Environment.MachineName); } if (command.IndexOf((char)34) != -1) //get additional parameters (in brackets) { start = command.IndexOf((char)34); detect = (char)34; } else if (command.IndexOf((char)39) != -1) { start = command.IndexOf((char)39); detect = (char)39; } if (detect != ' ') { shortened = command.Substring(start + 1); end = shortened.IndexOf(detect); st.Push(shortened.Substring(0, end)); found = true; command = command.Remove(start, end + 3); } foreach (string word in keywords) { if (command.IndexOf(word) != -1) { end = command.IndexOf(word) - 1; } } flag = false; start = 0; if (!found) //additional parameter without brackets { if (end != 0) { for (int i = end; i >= 0; i--) { if (i == 0) { start = i; } else if (command[i] == ' ') { foreach (string cmd in funcs) //search for command name without any parameter { if (cmd == command.Substring(0, end + 1)) { flag = true; st.Push(cmd); break; } } if (!flag) //get the parameter if the detected string didn't match any commmand { start = i + 1; st.Push(command.Substring(start, end - start + 1)); break; } } } } } try { param = command.Substring(0, end + 1); } catch { } if (!flag) //if no command was detected, searches and identifies a possibe command string { if (start != 0) { com = command.Substring(0, start - 1); //check command name } else if (end != 0) { com = param; } else { com = command; } foreach (string cmd in funcs) //checks if such a command does exist { if (com == cmd) { st.Push(com); //saves it if it does flag = true; break; } } if (!flag) { return new string[] { com } } ; //returns just the command to later inform the client about the mistake } string[] finite = new string[st.Count]; //organize and return a list from the stack int counter = 0; while (st.Count > 0) { finite[counter] = st.Pop(); counter++; } return(finite); } catch { return(new string[] { "command failed" }); } }