Beispiel #1
0
        private void OpenWithForm_Load(object sender, EventArgs e)
        {
            FormEx.Dockable(this);

            BackColor = Main.Colors.BaseDark;

            Icon = Resources.PortableApps_blue;

            Lang.SetControlLang(this);
            Text = Lang.GetText(Name);

            Main.SetFont(this);
            Main.SetFont(appMenu);

            var notifyBox = new NotifyBox {
                Opacity = .8d
            };

            notifyBox.Show(Lang.GetText(nameof(en_US.FileSystemAccessMsg)), Main.Title, NotifyBox.NotifyBoxStartPosition.Center);
            Main.CheckCmdLineApp();
            notifyBox.Close();
            if (WinApi.NativeHelper.GetForegroundWindow() != Handle)
            {
                WinApi.NativeHelper.SetForegroundWindow(Handle);
            }

            AppsBox_Update(false);
        }
Beispiel #2
0
        internal static void DownloadArchiver()
        {
            var notifyBox = new NotifyBox();

            notifyBox.Show(Language.GetText(nameof(en_US.InitRequirementsNotify)), Resources.GlobalTitle, NotifyBoxStartPosition.Center);
            var mirrors = NetEx.InternalDownloadMirrors;
            var verMap  = new Dictionary <string, string>();

            foreach (var mirror in mirrors)
            {
                try
                {
                    var url = PathEx.AltCombine(mirror, ".redists", "Extra", "Last.ini");
                    if (!NetEx.FileIsAvailable(url, 30000))
                    {
                        throw new PathNotFoundException(url);
                    }
                    var data = WebTransfer.DownloadString(url);
                    if (string.IsNullOrWhiteSpace(data))
                    {
                        throw new ArgumentNullException(nameof(data));
                    }
                    const string name = "7z";
                    if (string.IsNullOrEmpty(name))
                    {
                        continue;
                    }
                    var version = Ini.ReadOnly(name, "Version", data);
                    if (string.IsNullOrWhiteSpace(version))
                    {
                        continue;
                    }
                    verMap.Add(name, version);
                }
                catch (Exception ex) when(ex.IsCaught())
                {
                    Log.Write(ex);
                }
                if (verMap.Count > 0)
                {
                    break;
                }
            }
            foreach (var map in verMap)
            {
                var file = $"{map.Value}.zip";
                var path = PathEx.Combine(CachePaths.UpdateDir, file);
                foreach (var mirror in mirrors)
                {
                    try
                    {
                        var url = PathEx.AltCombine(mirror, ".redists", "Extra", map.Key, file);
                        if (!NetEx.FileIsAvailable(url, 30000))
                        {
                            throw new PathNotFoundException(url);
                        }
                        WebTransfer.DownloadFile(url, path);
                        Compaction.Unzip(path, CachePaths.UpdateDir);
                    }
                    catch (Exception ex) when(ex.IsCaught())
                    {
                        Log.Write(ex);
                    }
                    if (File.Exists(path))
                    {
                        break;
                    }
                }
            }
            notifyBox.Close();
        }
Beispiel #3
0
        public static void RedistHandling(Options options, params EnvironmentEx.Redist.Flags[] versions)
        {
            var dict = new Dictionary <EnvironmentEx.Redist.Flags, Dictionary <int, List <int> > >();

            foreach (var version in versions)
            {
                if (!dict.ContainsKey(version))
                {
                    dict.Add(version, new Dictionary <int, List <int> >());
                }
                switch (version)
                {
                case EnvironmentEx.Redist.Flags.VC2005X86:
                    if (!dict[version].ContainsKey(2005))
                    {
                        dict[version].Add(2005, new List <int>());
                    }
                    dict[version][2005].Add(86);
                    break;

                case EnvironmentEx.Redist.Flags.VC2005X64:
                    if (!dict[version].ContainsKey(2005))
                    {
                        dict[version].Add(2005, new List <int>());
                    }
                    dict[version][2005].Add(64);
                    break;

                case EnvironmentEx.Redist.Flags.VC2008X86:
                    if (!dict[version].ContainsKey(2008))
                    {
                        dict[version].Add(2008, new List <int>());
                    }
                    dict[version][2008].Add(86);
                    break;

                case EnvironmentEx.Redist.Flags.VC2008X64:
                    if (!dict[version].ContainsKey(2008))
                    {
                        dict[version].Add(2008, new List <int>());
                    }
                    dict[version][2008].Add(64);
                    break;

                case EnvironmentEx.Redist.Flags.VC2010X86:
                    if (!dict[version].ContainsKey(2010))
                    {
                        dict[version].Add(2010, new List <int>());
                    }
                    dict[version][2010].Add(86);
                    break;

                case EnvironmentEx.Redist.Flags.VC2010X64:
                    if (!dict[version].ContainsKey(2010))
                    {
                        dict[version].Add(2010, new List <int>());
                    }
                    dict[version][2010].Add(64);
                    break;

                case EnvironmentEx.Redist.Flags.VC2012X86:
                    if (!dict[version].ContainsKey(2012))
                    {
                        dict[version].Add(2012, new List <int>());
                    }
                    dict[version][2012].Add(86);
                    break;

                case EnvironmentEx.Redist.Flags.VC2012X64:
                    if (!dict[version].ContainsKey(2012))
                    {
                        dict[version].Add(2012, new List <int>());
                    }
                    dict[version][2012].Add(64);
                    break;

                case EnvironmentEx.Redist.Flags.VC2013X86:
                    if (!dict[version].ContainsKey(2013))
                    {
                        dict[version].Add(2013, new List <int>());
                    }
                    dict[version][2013].Add(86);
                    break;

                case EnvironmentEx.Redist.Flags.VC2013X64:
                    if (!dict[version].ContainsKey(2013))
                    {
                        dict[version].Add(2013, new List <int>());
                    }
                    dict[version][2013].Add(64);
                    break;

                case EnvironmentEx.Redist.Flags.VC2015X86:
                    if (!dict[version].ContainsKey(2015))
                    {
                        dict[version].Add(2015, new List <int>());
                    }
                    dict[version][2015].Add(86);
                    break;

                case EnvironmentEx.Redist.Flags.VC2015X64:
                    if (!dict[version].ContainsKey(2015))
                    {
                        dict[version].Add(2015, new List <int>());
                    }
                    dict[version][2015].Add(64);
                    break;

                case EnvironmentEx.Redist.Flags.VC2017X86:
                    if (!dict[version].ContainsKey(2017))
                    {
                        dict[version].Add(2017, new List <int>());
                    }
                    dict[version][2017].Add(86);
                    break;

                case EnvironmentEx.Redist.Flags.VC2017X64:
                    if (!dict[version].ContainsKey(2017))
                    {
                        dict[version].Add(2017, new List <int>());
                    }
                    dict[version][2017].Add(64);
                    break;

                default:
                    return;
                }
            }
            foreach (var data in dict)
            {
                var version = data.Key;
                foreach (var vars in data.Value)
                {
                    var year = vars.Key;
                    foreach (var arch in vars.Value)
                    {
                        var path = PathEx.Combine(PathEx.LocalDir, $"_CommonRedist\\vcredist\\{year}\\vcredist_x{arch}.exe");
                        if (!File.Exists(path))
                        {
                            return;
                        }
                        var iniPath = Path.ChangeExtension(PathEx.LocalPath, ".ini");
                        switch (options)
                        {
                        case Options.Exit:
                            if (Ini.ReadDirect("Redist", version.ToString(), iniPath).EqualsEx("True"))
                            {
                                return;
                            }
                            using (var p = ProcessEx.Start(path, "/q /uninstall /norestart", Elevation.IsAdministrator, false))
                                if (p?.HasExited == false)
                                {
                                    p.WaitForExit();
                                }
                            break;

                        default:
                            if (Ini.ReadDirect("Redist", version.ToString(), iniPath).EqualsEx("False"))
                            {
                                Elevation.RestartAsAdministrator(EnvironmentEx.CommandLine(false));
                            }
                            if (EnvironmentEx.Redist.IsInstalled(version))
                            {
                                Ini.WriteDirect("Redist", version.ToString(), true, iniPath);
                                break;
                            }
                            var info = FileVersionInfo.GetVersionInfo(PathEx.LocalPath);
                            if (!Ini.ReadDirect("Redist", version.ToString(), iniPath).EqualsEx("True", "False"))
                            {
                                MessageBoxEx.TopMost = true;
                                MessageBoxEx.ButtonText.OverrideEnabled = true;
                                MessageBoxEx.ButtonText.Yes             = "&Yes";
                                MessageBoxEx.ButtonText.No     = "&No";
                                MessageBoxEx.ButtonText.Cancel = "&Cancel";
                                var msg    = $"Microsoft Visual C++ {year} Redistributable Package (x{arch}) is required to run this porgram.{Environment.NewLine}{Environment.NewLine}Would you like to permanently install this package (yes) or temporarily (no)?";
                                var result = MessageBoxEx.Show(msg, info.FileDescription, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                                if (result == DialogResult.Cancel)
                                {
                                    RedistHandling(Options.Exit, versions);
                                    Environment.Exit(Environment.ExitCode);
                                }
                                Ini.WriteDirect("Redist", version.ToString(), result == DialogResult.Yes, iniPath);
                                if (result != DialogResult.Yes)
                                {
                                    Elevation.RestartAsAdministrator(EnvironmentEx.CommandLine(false));
                                }
                            }
                            var notifyBox = new NotifyBox();
                            notifyBox.Show($"Microsoft Visual C++ {year} Redistributable Package (x{arch}) has been initialized . . .", info.FileDescription, NotifyBox.NotifyBoxStartPosition.Center);
                            using (var p = ProcessEx.Start(path, "/q /norestart", Elevation.IsAdministrator, false))
                                if (p?.HasExited == false)
                                {
                                    p.WaitForExit();
                                }
                            notifyBox.Close();
                            if (!EnvironmentEx.Redist.IsInstalled(version))
                            {
                                Environment.ExitCode = 1;
                                Environment.Exit(Environment.ExitCode);
                            }
                            break;
                        }
                    }
                }
            }
        }
Beispiel #4
0
        private static void RegistryHelper(string appDir, bool entries)
        {
            var notifyBox = new NotifyBox {
                TopMost = true
            };

            if (!entries)
            {
                if (!Elevation.IsAdministrator || !Environment.CommandLine.ContainsEx("{CFFB1200-8B38-451D-80A0-BE187E64EC61}"))
                {
                    return;
                }

                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                notifyBox.Show("Registry entries are being removed . . .",
#if x86
                               "WinRAR Portable",
#else
                               "WinRAR Portable (64-bit)",
#endif
                               NotifyBox.NotifyBoxStartPosition.Center);

                foreach (var key in new[] { Registry.ClassesRoot, Registry.CurrentUser, Registry.LocalMachine })
                {
                    var subKeys = Reg.GetSubKeyTree(key, null, 20000)?.Where(x => x.ContainsEx("WinRAR") || RegExists(key, x, "WinRAR")).ToArray();
                    if (subKeys?.Any(x => RegExists(key, x, "Portable")) != true)
                    {
                        continue;
                    }
                    foreach (var subKey in subKeys)
                    {
                        Reg.RemoveSubKey(key, subKey);
                    }
                }
                notifyBox.Close();
                Environment.ExitCode = 0;
                Environment.Exit(Environment.ExitCode);
            }

            var oldDir = Ini.Read("History", "AppDirectory");
            if (oldDir.EqualsEx(appDir))
            {
                return;
            }

            if (!Elevation.IsAdministrator)
            {
                ProcessEx.Start(PathEx.LocalPath, PathEx.LocalDir, "{5C1A7995-36C7-4346-B549-D6392EFC86E4}", true, false)?.WaitForExit();
                return;
            }

            if (!Ini.Read("History", "FirstTime", false))
            {
#if x86
                oldDir = PathEx.Combine(PathEx.LocalDir, "winrar");
#else
                oldDir = PathEx.Combine(PathEx.LocalDir, "winrar-x64");
#endif
            }

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            notifyBox.Show("Registry entries are being repaired . . .",
#if x86
                           "WinRAR Portable",
#else
                           "WinRAR Portable (64-bit)",
#endif
                           NotifyBox.NotifyBoxStartPosition.Center);

            try
            {
                foreach (var key in new[] { Registry.ClassesRoot, Registry.CurrentUser, Registry.LocalMachine })
                {
                    var subKeys = Reg.GetSubKeyTree(key, null, 20000)?.Where(x => RegExists(key, x, oldDir));
                    if (subKeys == null)
                    {
                        continue;
                    }
                    foreach (var subKey in subKeys)
                    {
                        var oldValue = Reg.ReadString(key, subKey, null);
                        if (oldValue.ContainsEx(oldDir))
                        {
                            Reg.Write(key, subKey, null, oldValue.Replace(oldDir, appDir));
                        }
                        oldValue = Reg.ReadString(key, subKey, "FileName");
                        if (!oldValue.ContainsEx(oldDir))
                        {
                            continue;
                        }
                        Reg.Write(key, subKey, "FileName", oldValue.Replace(oldDir, appDir));
                    }
                }
                Ini.Write("History", "AppDirectory", appDir);
                Ini.Write("History", "FirstTime", true);
                Ini.WriteAll();
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            notifyBox.Close();
            if (!Environment.CommandLine.ContainsEx("{5C1A7995-36C7-4346-B549-D6392EFC86E4}"))
            {
                return;
            }
            Environment.ExitCode = 0;
            Environment.Exit(Environment.ExitCode);
        }
        /// <summary>
        ///     Enables/disables the specified redistributable packages.
        /// </summary>
        /// <param name="option">
        ///     The option that determines whether the redistributable packages will be
        ///     enabled or disabled.
        /// </param>
        /// <param name="versions">
        ///     The redistributable package versions to handle.
        /// </param>
        public static void Handler(PortalizerActions option, params EnvironmentEx.RedistFlags[] versions)
        {
            if (versions == null)
            {
                return;
            }
            var dict = new Dictionary <EnvironmentEx.RedistFlags, Dictionary <int, List <int> > >();

            foreach (var version in versions)
            {
                try
                {
                    var data = Enum.GetName(typeof(EnvironmentEx.RedistFlags), version)?.Split('X')
                               .Select(s => new string(s.Where(char.IsDigit).ToArray())).ToArray();
                    if (data == null)
                    {
                        throw new ArgumentNullException(nameof(data));
                    }
                    if (data.Length != 2)
                    {
                        throw new ArgumentOutOfRangeException(nameof(data));
                    }
                    var year = Convert.ToInt32(data.First());
                    if (year < 2005)
                    {
                        throw new ArgumentOutOfRangeException(nameof(year));
                    }
                    var arch = Convert.ToInt32(data.Last());
                    if (arch != 64 && arch != 86)
                    {
                        throw new ArgumentOutOfRangeException(nameof(arch));
                    }
                    if (!dict.ContainsKey(version))
                    {
                        dict.Add(version, new Dictionary <int, List <int> >());
                    }
                    if (!dict[version].ContainsKey(year))
                    {
                        dict[version].Add(year, new List <int>());
                    }
                    dict[version][year].Add(arch);
                }
                catch (Exception ex)
                {
                    Log.Write(ex);
                }
            }
            foreach (var data in dict)
            {
                var version = data.Key;
                if (version == EnvironmentEx.RedistFlags.VC2005X86 ||
                    version == EnvironmentEx.RedistFlags.VC2005X64)
                {
                    continue;
                }
                foreach (var vars in data.Value)
                {
                    var year = vars.Key;
                    foreach (var arch in vars.Value)
                    {
                        var path = PathEx.Combine(PathEx.LocalDir, $"_CommonRedist\\vcredist\\{year}\\vcredist_x{arch}.exe");
                        if (!File.Exists(path))
                        {
                            return;
                        }
                        var    iniPath = Path.ChangeExtension(PathEx.LocalPath, ".ini");
                        string args;
                        switch (option)
                        {
                        case PortalizerActions.Disable:
                            if (Ini.ReadDirect("Redist", version.ToString(), iniPath).EqualsEx("True"))
                            {
                                return;
                            }
                            switch (year)
                            {
                            case 2008:
                                args = "/qu";
                                break;

                            case 2010:
                            case 2012:
                                args = "/uninstall /q /norestart";
                                break;

                            default:
                                args = "/uninstall /quiet /norestart";
                                break;
                            }
                            using (var p = ProcessEx.Start(path, args, Elevation.IsAdministrator, false))
                                if (p?.HasExited == false)
                                {
                                    p.WaitForExit();
                                }
                            break;

                        default:
                            if (Ini.ReadDirect("Redist", version.ToString(), iniPath).EqualsEx("False"))
                            {
                                Elevation.RestartAsAdministrator(EnvironmentEx.CommandLine(false));
                            }
                            if (EnvironmentEx.Redist.IsInstalled(version))
                            {
                                Ini.WriteDirect("Redist", version.ToString(), true, iniPath);
                                break;
                            }
                            if (!Ini.ReadDirect("Redist", version.ToString(), iniPath).EqualsEx("True", "False"))
                            {
                                MessageBoxEx.TopMost = true;
                                MessageBoxEx.ButtonText.OverrideEnabled = true;
                                MessageBoxEx.ButtonText.Yes             = Strings.RedistRequestButtonYes;
                                MessageBoxEx.ButtonText.No     = Strings.RedistRequestButtonNo;
                                MessageBoxEx.ButtonText.Cancel = Strings.RedistRequestButtonCancel;
                                var msg    = string.Format(Strings.RedistRequestMessage, year, arch);
                                var result = MessageBoxEx.Show(msg, AssemblyInfo.Title, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                                if (result == DialogResult.Cancel)
                                {
                                    Handler(PortalizerActions.Disable, versions);
                                    Environment.Exit(Environment.ExitCode);
                                }
                                Ini.WriteDirect("Redist", version.ToString(), result == DialogResult.Yes, iniPath);
                                if (result != DialogResult.Yes)
                                {
                                    Elevation.RestartAsAdministrator(EnvironmentEx.CommandLine(false));
                                }
                            }
                            var notifyBox = new NotifyBox();
                            notifyBox.Show(string.Format(Strings.RedistProgressNotify, year, arch), AssemblyInfo.Title, NotifyBoxStartPosition.Center);
                            switch (year)
                            {
                            case 2008:
                                args = "/q";
                                break;

                            case 2010:
                            case 2012:
                                args = "/q /norestart";
                                break;

                            default:
                                args = "/install /quiet /norestart";
                                break;
                            }
                            using (var p = ProcessEx.Start(path, args, Elevation.IsAdministrator, false))
                                if (p?.HasExited == false)
                                {
                                    p.WaitForExit();
                                }
                            notifyBox.Close();
                            if (!EnvironmentEx.Redist.IsInstalled(version))
                            {
                                Environment.ExitCode = 1;
                                Environment.Exit(Environment.ExitCode);
                            }
                            break;
                        }
                    }
                }
            }
        }