private void CybeSystemsRunCygwin_Click(object sender, EventArgs e, string fileName)
 {
     Cygwin.CygwinOpen(fileName);
 }
        public Form_TrayMenu()
        {
            //Check Arguments First -> Dont load GUI
            string[] args = Environment.GetCommandLineArgs();

            try
            {
                if (args[1] != null)
                {
                    Console.WriteLine("CommandLine Parameter 1: " + args[1]);
                    if (!args[1].StartsWith("-"))
                    {
                        Cygwin.CygwinOpen(args[1]);
                        Environment.Exit(-1);
                    }
                    else if (args[1].StartsWith("-uactask"))
                    {
                        ChangeRegistryPath.Change();
                        Environment.Exit(-1);
                    }
                    else
                    {
                        string path             = "";
                        string exitAfterExecute = "0";
                        int    idx = 0;

                        foreach (string arg in args)
                        {
                            if (args[idx] == "-path")
                            {
                                path = args[idx + 1];
                            }

                            if (args[idx] == "-wsl")
                            {
                                Globals.CurrentEnvironment = "wsl";
                            }

                            if (args[idx] == "-cygwin")
                            {
                                Globals.CurrentEnvironment = "cygwin";
                            }

                            if (args[idx] == "-exit")
                            {
                                if (args[idx + 1] != null)
                                {
                                    exitAfterExecute = args[idx + 1];
                                    Console.WriteLine("exitAfterExecute set to: " + exitAfterExecute);
                                }
                            }
                            idx = idx + 1;
                        }

                        if (exitAfterExecute == "1")
                        {
                            Globals.MainConfig["Cygwin"]["ExitAfterExec"] = true;
                        }


                        //MessageBox.Show("Test" + path, "Test", MessageBoxButtons.YesNo);
                        Cygwin.CygwinOpen(path);

                        Environment.Exit(-1);
                    }
                }
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("Possible Commandline options: " + args[0] + " [-cygwin] [-wsl] [-path <PATH>] [-exit <0/1>]");
                Console.WriteLine("-path 'C:\\Windows' open Windows folder");
                Console.WriteLine("-exit 0 let the cygwin window open, -exit 1 close the cygwin window after execution");
                Console.WriteLine("");
                Console.WriteLine("Sample to open C:\\Windows Folder:");
                Console.WriteLine("   " + args[0] + " -path 'C:\\Windows'");
                Console.WriteLine("Sample to open Shellscript and dont close window after execution:");
                Console.WriteLine("   " + args[0] + " -path 'C:\\shellscript.sh' -exit 0");
                Console.WriteLine("Sample to open C:\\Windows Folder WITHOUT arguemnts:");
                Console.WriteLine("   " + args[0] + " 'C:\\Windows'");
                Console.WriteLine("Sample to open C:\\Windows Folder with cygwin:");
                Console.WriteLine("   " + args[0] + " -cygwin -path 'C:\\Windows'");
                Console.WriteLine("Sample to open C:\\Windows Folder with wsl:");
                Console.WriteLine("   " + args[0] + " -wsl -path 'C:\\Windows'");
            }

            ChangeRegistryPath.Change();
            //Check Arguments Ended -> If CygwinPortable still lives -> go on
            InitializeComponent();

            WindowState = FormWindowState.Minimized;
            if ((WindowState == FormWindowState.Minimized))
            {
                contextMenuStrip1.Show();
                Hide();
                ShowInTaskbar = false;
            }
        }
Exemple #3
0
        public static void CygwinOpen(string fileOrFolder)
        {
            string[] cygFolder = new string[5];

            if (fileOrFolder == "~")
            {
                cygFolder[0] = "~";
                cygFolder[1] = "folder";
                cygFolder[2] = "";
                cygFolder[3] = "";
                cygFolder[4] = "";
            }
            else
            {
                cygFolder = Cygwin.Folder2CygFolder(fileOrFolder);
                Console.WriteLine("Fullpath " + cygFolder[0]);
                Console.WriteLine("FileOrFolder " + cygFolder[1]);
                Console.WriteLine("Path " + cygFolder[2]);
                Console.WriteLine("File " + cygFolder[3]);
                Console.WriteLine("Extension " + cygFolder[4]);
            }


            if (Globals.CurrentEnvironment == "cygwin")
            {
                if (!Directory.Exists(Globals.AppPath + "\\Runtime\\cygwin\\home\\" + (string)Globals.MainConfig["Cygwin"]["Username"]))
                {
                    Globals.MainConfig["Cygwin"]["Shell"] = "mintty";
                    if (File.Exists(Globals.AppPath + "\\Runtime\\cygwin\\bin\\bash.exe"))
                    {
                        Directory.CreateDirectory(Globals.AppPath + "\\Runtime\\cygwin\\home\\" + (string)Globals.MainConfig["Cygwin"]["Username"]);
                    }
                }
            }

            Console.WriteLine(Environment.GetEnvironmentVariable("PATH"));
            Console.WriteLine(Environment.GetEnvironmentVariable("PATH"));
            Console.WriteLine(Environment.GetEnvironmentVariable("HOME"));
            Console.WriteLine(Environment.GetEnvironmentVariable("USERNAME"));

            string shellStayOpen = "";
            string path          = "";
            string parameter     = "";
            string pathname      = Globals.AppPath;

            if ((bool)Globals.MainConfig["Cygwin"]["ExitAfterExec"] == false)
            {
                shellStayOpen = ";exec /bin/bash'";
            }
            else
            {
                shellStayOpen = "'";
            }

            if (cygFolder[1] == "file")
            {
                string[] extensions = ((string)Globals.MainConfig["Cygwin"]["ExecutableExtension"]).Split(',');
                if (((IList <string>)extensions).Contains(cygFolder[4].Replace(".", "")))
                {
                    Console.WriteLine("Extension valid -> Executing");
                    string executeCommand = ";./" + cygFolder[3];


                    if ((string)Globals.MainConfig["Cygwin"]["Shell"] == "ConEmu")
                    {
                        if (Globals.CurrentEnvironment == "wsl")
                        {
                            path      = Globals.AppPath + "\\Runtime\\ConEmu\\ConEmu.exe";
                            parameter = " /cmd " + "bash -i -c 'cd " + cygFolder[2] + executeCommand + shellStayOpen;
                        }
                        else
                        {
                            path      = Globals.AppPath + "\\Runtime\\ConEmu\\ConEmu.exe";
                            parameter = " /cmd " + Globals.AppPath + "\\Runtime\\cygwin\\bin\\bash.exe --login -i -c 'cd " + cygFolder[2] + executeCommand + shellStayOpen;
                        }
                    }
                    else
                    {
                        if (Globals.CurrentEnvironment == "wsl")
                        {
                            path      = "cmd.exe";
                            parameter = " /k bash -c 'cd " + cygFolder[2] + executeCommand + shellStayOpen;
                        }
                        else
                        {
                            path      = Globals.AppPath + "\\Runtime\\cygwin\\bin\\mintty.exe";
                            parameter = " --config /home/" + (string)Globals.MainConfig["Cygwin"]["Username"] + "/.minttyrc -e /bin/bash.exe -c 'cd " + cygFolder[2] + executeCommand + shellStayOpen;
                        }
                    }

                    pathname = Globals.AppPath;

                    Process process     = new Process();
                    var     processInfo = new ProcessStartInfo();
                    process.StartInfo.UseShellExecute = false;
                    if (Globals.CurrentEnvironment == "wsl")
                    {
                        process.StartInfo.Arguments = parameter + " -new_console:C:\"" + Globals.AppPath + "\\AppInfo\\ubuntu.ico\"";
                    }
                    else
                    {
                        process.StartInfo.Arguments = parameter + " -new_console:C:\"" + Globals.AppPath + "\\AppInfo\\appicon.ico\"";
                    }
                    process.StartInfo.FileName = path;
                    //process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;

                    //Workaround for Case Sensitive variables in .NET 2.0-3.5 -> Its fixed in .NET 4.0
                    //Not really beautiful but working...
                    System.Reflection.FieldInfo  contentsField = typeof(System.Collections.Specialized.StringDictionary).GetField("contents", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                    System.Collections.Hashtable environment   = (System.Collections.Hashtable)contentsField.GetValue(process.StartInfo.EnvironmentVariables);

                    if (Directory.Exists(Globals.PortableAppsPath + "\\PortableApps"))
                    {
                        Console.WriteLine("Running in PortableApps Environment");
                        environment.Remove("PORTABLEAPPS");
                        environment.Add("PORTABLEAPPS", "true");
                    }
                    else
                    {
                        environment.Remove("PORTABLEAPPS");
                        environment.Add("PORTABLEAPPS", "false");
                    }

                    string pathvar = Environment.GetEnvironmentVariable("PATH");
                    if ((bool)Globals.MainConfig["Cygwin"]["WindowsPathToCygwin"])
                    {
                        environment.Remove("PATH");
                        environment.Add("PATH", pathvar + ";" + (string)Globals.MainConfig["Cygwin"]["WindowsAdditionalPath"] + ";" + Globals.AppPath + "\\Runtime\\cygwin\\bin");
                    }
                    else
                    {
                        environment.Remove("PATH");
                        environment.Add("PATH", pathvar + ";" + Globals.AppPath + "\\Runtime\\cygwin\\bin");
                    }
                    environment.Remove("ALLUSERSPROFILE");
                    environment.Add("ALLUSERSPROFILE", "C:\\ProgramData");
                    environment.Remove("ProgramData");
                    environment.Add("ProgramData", "C:\\ProgramData");
                    environment.Remove("CYGWIN_HOME");
                    environment.Add("CYGWIN_HOME", Globals.AppPath + "\\Runtime\\cygwin");
                    environment.Remove("USER");
                    environment.Add("USER", (string)Globals.MainConfig["Cygwin"]["Username"]);
                    environment.Remove("USERNAME");
                    environment.Add("USERNAME", (string)Globals.MainConfig["Cygwin"]["Username"]);
                    environment.Remove("HOME");
                    environment.Add("HOME", "/home/" + (string)Globals.MainConfig["Cygwin"]["Username"]);
                    environment.Remove("USBDRV");
                    environment.Add("USBDRV", Path.GetPathRoot(Globals.AppPath));
                    environment.Remove("USBDRVPATH");
                    environment.Add("USBDRVPATH", Path.GetPathRoot(Globals.AppPath));
                    if ((string)Globals.MainConfig["Cygwin"]["WindowsPythonPath"] != "")
                    {
                        environment.Remove("PYTHONPATH");
                        environment.Add("PYTHONPATH", (string)Globals.MainConfig["Cygwin"]["WindowsPythonPath"]);
                    }
                    //This is the normal .NET way -> Dont works in 2.0 $HOME is $home in cygwin
                    //process.StartInfo.EnvironmentVariables["USER"] = "******";
                    process.Start();
                }
            }
            if (cygFolder[1] == "folder")
            {
                if ((string)Globals.MainConfig["Cygwin"]["Shell"] == "ConEmu")
                {
                    if (Globals.CurrentEnvironment == "wsl")
                    {
                        path      = Globals.AppPath + "\\Runtime\\ConEmu\\ConEmu.exe";
                        parameter = " /cmd " + "bash -i -c 'cd " + cygFolder[0] + shellStayOpen;
                    }
                    else
                    {
                        path      = Globals.AppPath + "\\Runtime\\ConEmu\\ConEmu.exe";
                        parameter = " /cmd " + Globals.AppPath + "\\Runtime\\cygwin\\bin\\bash.exe --login -i -c 'cd " + cygFolder[0] + shellStayOpen;
                    }
                }
                else
                {
                    if (Globals.CurrentEnvironment == "wsl")
                    {
                        path      = "cmd.exe";
                        parameter = " /k bash -c 'cd " + cygFolder[0] + shellStayOpen;
                    }
                    else
                    {
                        path      = Globals.AppPath + "\\Runtime\\cygwin\\bin\\mintty.exe";
                        parameter = " --config /home/" + (string)Globals.MainConfig["Cygwin"]["Username"] + "/.minttyrc -e /bin/bash.exe -c 'cd " + cygFolder[0] + shellStayOpen;
                    }
                }

                pathname = Globals.AppPath;
                Process process     = new Process();
                var     processInfo = new ProcessStartInfo();
                process.StartInfo.UseShellExecute = false;
                if (Globals.CurrentEnvironment == "wsl")
                {
                    process.StartInfo.Arguments = parameter + " -new_console:C:\"" + Globals.AppPath + "\\AppInfo\\ubuntu.ico\"";
                }
                else
                {
                    process.StartInfo.Arguments = parameter + " -new_console:C:\"" + Globals.AppPath + "\\AppInfo\\appicon.ico\"";
                }
                process.StartInfo.FileName = path;
                //process.StartInfo.WorkingDirectory = Environment.CurrentDirectory;
                System.Reflection.FieldInfo  contentsField = typeof(System.Collections.Specialized.StringDictionary).GetField("contents", System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.NonPublic);
                System.Collections.Hashtable environment   = (System.Collections.Hashtable)contentsField.GetValue(process.StartInfo.EnvironmentVariables);

                if (Directory.Exists(Globals.PortableAppsPath + "\\PortableApps"))
                {
                    Console.WriteLine("Running in PortableApps Environment");
                    environment.Remove("PORTABLEAPPS");
                    environment.Add("PORTABLEAPPS", "true");
                }
                else
                {
                    environment.Remove("PORTABLEAPPS");
                    environment.Add("PORTABLEAPPS", "false");
                }

                string pathvar = Environment.GetEnvironmentVariable("PATH");
                if ((bool)Globals.MainConfig["Cygwin"]["WindowsPathToCygwin"])
                {
                    environment.Remove("PATH");
                    environment.Add("PATH", pathvar + ";" + (string)Globals.MainConfig["Cygwin"]["WindowsAdditionalPath"] + ";" + Globals.AppPath + "\\Runtime\\cygwin\\bin");
                }
                else
                {
                    environment.Remove("PATH");
                    environment.Add("PATH", pathvar + ";" + Globals.AppPath + "\\Runtime\\cygwin\\bin");
                }
                environment.Remove("ALLUSERSPROFILE");
                environment.Add("ALLUSERSPROFILE", "C:\\ProgramData");
                environment.Remove("ProgramData");
                environment.Add("ProgramData", "C:\\ProgramData");
                environment.Remove("CYGWIN_HOME");
                environment.Add("CYGWIN_HOME", Globals.AppPath + "\\Runtime\\cygwin");
                environment.Remove("USER");
                environment.Add("USER", (string)Globals.MainConfig["Cygwin"]["Username"]);
                environment.Remove("USERNAME");
                environment.Add("USERNAME", (string)Globals.MainConfig["Cygwin"]["Username"]);
                environment.Remove("HOME");
                environment.Add("HOME", "/home/" + (string)Globals.MainConfig["Cygwin"]["Username"]);
                environment.Remove("USBDRV");
                environment.Add("USBDRV", Path.GetPathRoot(Globals.AppPath));
                environment.Remove("USBDRVPATH");
                environment.Add("USBDRVPATH", Path.GetPathRoot(Globals.AppPath));
                if ((string)Globals.MainConfig["Cygwin"]["WindowsPythonPath"] != "")
                {
                    environment.Remove("PYTHONPATH");
                    environment.Add("PYTHONPATH", (string)Globals.MainConfig["Cygwin"]["WindowsPythonPath"]);
                }
                process.Start();
            }
        }