//public bool IsAdministrator() //{ // WindowsIdentity identity = WindowsIdentity.GetCurrent(); // WindowsPrincipal principal = new WindowsPrincipal(identity); // return principal.IsInRole(WindowsBuiltInRole.Administrator); //} //public void restartAsAdmin() //{ // if (IsAdministrator() == false) // { // try // { // // Restart program and run as admin // var exeName = System.Diagnostics.Process.GetCurrentProcess().MainModule.FileName; // ProcessStartInfo startInfo = new ProcessStartInfo(exeName); // startInfo.Verb = "runas"; // System.Diagnostics.Process.Start(startInfo); // Environment.Exit(0); // return; // } // catch (Exception e) // { // if (e.ToString().Contains("The operation was canceled by the user")) // { // Console.WriteLine("Rashell: Operation unsuccessful."); // format.ConsoleColorWrite("User Denied Operation", ConsoleColor.Red, false); // } else // { // Console.WriteLine("Rashell: Operation unsuccessful."); // format.ConsoleColorWrite("Unknown Error", ConsoleColor.Yellow, false); // } // } // } //} #endregion "Loaders" #region "Argument Handlers" private List <char> getSwitches(String[] args) { List <char> switches = new List <char>(); foreach (string arg in args) { try { string buffer = format.RemoveSpace(arg.ToLower()); if (buffer.StartsWith("-") || buffer.StartsWith("/")) { buffer = buffer.Substring(1); if (!buffer.StartsWith("-")) //checks it is not an option which has two: (--). { foreach (char sw in buffer) { switches.Add(sw); } } } } catch (Exception e) { } } return(switches); }
/// <summary> /// Changes the current directory /// </summary> /// <param name="directory">The directory to be working into.</param> /// <param name="dir">holds a formatted version of the directory path.</param> /// <returns></returns> public bool cd(List <string> directory) { Formatters format = new Formatters(); Rashell shell = new Rashell(); if (directory.Count != 0) { try { string dir = format.RemoveSpace(directory[0].ToLower()); if (!string.IsNullOrEmpty(dir) && !string.IsNullOrWhiteSpace(dir) && Directory.Exists(dir)) { Directory.SetCurrentDirectory(dir); int CountSlash = 0; //Formats the shell working directory. foreach (char slash in Directory.GetCurrentDirectory()) { if (slash.ToString() == "\\") { CountSlash++; } } string dirname = Directory.GetCurrentDirectory().ToString(); string workdir; if (CountSlash != 1) { if (!string.IsNullOrEmpty(dirname) || !string.IsNullOrWhiteSpace(dirname)) { workdir = shell.setShellWorkingDirectory(shell.getSessionUser(), dirname); } else { workdir = shell.setShellWorkingDirectory(shell.getSessionUser(), dir); } } else { workdir = shell.setShellWorkingDirectory(shell.getSessionUser(), Directory.GetCurrentDirectory()); } Rashell.ShellSessionDirectory = workdir; } else { Console.WriteLine("The directory you specified is invalid."); } } catch (Exception) { } } return(true); }
/// <summary> /// Reads the configuration file and load the parameters /// </summary> /// <param name="FoundEnvi">Flag: Used to tell the program that en environment path is found.</param> /// <param name="FoundKnown">Flag: Used to tell the program that a executable extension is found.</param> /// <param name="Working_Dir">Tmp hold the DEF_WORKING_DIR path.</param> /// <param name="ElevationRequested">Whether Rights Elevation was required.</param> public bool Read() { bool FoundEnvi = false; bool FoundKnown_Ex = false; string Working_Dir = null; bool ElevationRequested = false; Start: if (File.Exists(config_path)) { StreamReader ConfigReader = new StreamReader(this.config_path); while (!ConfigReader.EndOfStream) { string line = ConfigReader.ReadLine().ToUpper(); line = format.RemoveTab(line); line = format.RemoveSpace(line); if (!string.IsNullOrEmpty(line)) { if (line.StartsWith("ENVIRONMENT_PATHS {")) { FoundEnvi = true; } else if (line.StartsWith("KNOWN_EXECUTABLES {")) { FoundKnown_Ex = true; } else if (line.StartsWith("DEF_WORKING_DIR =")) { if (line.EndsWith(";")) { Working_Dir = line.Substring(line.IndexOf("=") + 1, (line.Length - (line.IndexOf("=") + 1)) - 1); if (!string.IsNullOrEmpty(Working_Dir) && !string.IsNullOrWhiteSpace(Working_Dir)) { Working_Dir = format.Break(Working_Dir); if (Directory.Exists(Working_Dir)) { this.DEF_WORKING_DIR = Working_Dir; } } } } else if (line.StartsWith("ENABLE_EVT_LOGGING:")) { if (line.EndsWith(";")) { string log = null; log = line.Substring(line.IndexOf(":") + 2, (line.Length - (line.IndexOf(":") + 1)) - 2); if (log == "ON") { this.ENABLE_EVT_LOG = true; } else if (log == "OFF") { this.ENABLE_EVT_LOG = false; } } } else if (line.StartsWith("ENABLE_ERR_LOGGING:")) { if (line.EndsWith(";")) { string log = null; log = line.Substring(line.IndexOf(":") + 2, (line.Length - (line.IndexOf(":") + 1)) - 2); if (log == "ON") { this.ENABLE_ERR_LOG = true; } else if (log == "OFF") { this.ENABLE_ERR_LOG = false; } } } else if (line.StartsWith("PRIORITIZE_BUILTIN_SHELL:")) { if (line.EndsWith(";")) { string log = null; log = line.Substring(line.IndexOf(":") + 2, (line.Length - (line.IndexOf(":") + 1)) - 2); if (log == "ON") { this.PRIORITIZE_BUILTIN_SHELL = true; } else if (log == "OFF") { this.PRIORITIZE_BUILTIN_SHELL = false; } } } else if (line.StartsWith("WELCOME_MESSAGE:")) { if (line.EndsWith(";")) { string log = null; log = line.Substring(line.IndexOf(":") + 2, (line.Length - (line.IndexOf(":") + 1)) - 2); if (log == "ON") { this.DISPLAY_WELCOME_MSG = true; } else if (log == "OFF") { this.DISPLAY_WELCOME_MSG = false; } } } else if (FoundEnvi) { string envipath = null; if (line.EndsWith(";") && line.Length > 1) { envipath = line.Substring(0, line.Length - 1); envipath = format.Break(envipath); if (Directory.Exists(envipath)) { this.EnvironmentPaths.Add(envipath); } } else if (line == "}") { FoundEnvi = false; } } else if (FoundKnown_Ex) { string extension = null; if (line.EndsWith(";") && line.Length > 1) { extension = line.Substring(0, line.Length - 1); if (extension.StartsWith(".")) { this.Known_Extensions.Add(extension); } } else if (line == "}") { FoundKnown_Ex = false; } } } } ConfigReader.Close(); } else { try { Generate(); } catch (Exception e) { if (e.ToString().Contains("requires elevation")) { if (!ElevationRequested) { try { ElevationRequested = true; Rashell shell = new Rashell(); shell.restartAsAdmin(); } catch (Exception x) { } } else { Environment.Exit(1); } } } goto Start; } return(true); }
public bool Execute(string cmd, List <string> arguments) { string Arguments = null; foreach (string arg in arguments) { Arguments += " " + arg; } Process process = new Process(); ProcessStartInfo property = new ProcessStartInfo(cmd) { RedirectStandardError = true, RedirectStandardOutput = true, RedirectStandardInput = true, UseShellExecute = false, CreateNoWindow = true, Arguments = Arguments, }; process.StartInfo = property; // Depending on your application you may either prioritize the IO or the exact opposite Thread outputThread = new Thread(outputReader) { Name = "ChildIO Output", IsBackground = true }; Thread errorThread = new Thread(errorReader) { Name = "ChildIO Error", IsBackground = true }; Thread inputThread = new Thread(inputReader) { Name = "ChildIO Input", IsBackground = true }; // Start the IO threads try { process.Start(); //start reader threads outputThread.Start(process); errorThread.Start(process); inputThread.Start(process); } catch (Exception x) { if (x.ToString().Contains("requires elevation")) { Console.WriteLine("Rashell: Unable to start application \"" + cmd + "\"" + "."); format.ConsoleColorWrite("Requires Elevation", ConsoleColor.Red, false); Console.Write("You want to restart Rashell with Elevated Priviledges? (Y/N):"); string reply = Console.ReadLine().ToLower(); reply = format.RemoveTab(reply); reply = format.RemoveSpace(reply); if (reply == "y" || reply == "yes") { //shell.restartAsAdmin(); } else { goto KillThreads; } } else if (x.ToString().Contains("specified executable is not a valid")) { Console.WriteLine("Rashell: Unable to start \"" + cmd + "\"" + "."); format.ConsoleColorWrite("Invalid File Type", ConsoleColor.Red, false); goto KillThreads; } else { Console.WriteLine("Rashell: Unable to start \"" + cmd + "\"" + "."); format.ConsoleColorWrite("Unknown Error", ConsoleColor.Yellow, false); goto KillThreads; } } // Signal to end the application ManualResetEvent stopApp = new ManualResetEvent(false); // Enables the exited event and set the stopApp signal on exited process.EnableRaisingEvents = true; process.Exited += (e, sender) => { stopApp.Set(); }; // Wait for the child app to stop stopApp.WaitOne(); // Kill all started threads when child ends. KillThreads: return(true); }