/// <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);
        }
Exemple #2
0
        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);
        }