예제 #1
0
        public static void Find(out string javaPath)
        {
            // Read saved path from config.
            javaPath = null;
            if (File.Exists(Attributes.ConfigPath))
            {
                javaPath = Ini.ReadDirect("Java", "Path", Attributes.ConfigPath);
                if (!string.IsNullOrWhiteSpace(javaPath))
                {
                    javaPath = PathEx.Combine(javaPath);
                }
                if (File.Exists(javaPath))
                {
                    goto Found;
                }
            }

            // Try getting the default path from Portable Apps Suite JRE installation.
            try
            {
                var dir = EnvironmentEx.GetVariableValue("AppsSuiteDir");
                if (Directory.Exists(dir))
                {
#if x86
                    dir = Path.Combine(dir, "Apps", "CommonFiles", "Java");
#else
                    dir = Path.Combine(dir, "Apps", "CommonFiles", "Java64");
                    if (!Directory.Exists(dir))
                    {
                        Path.Combine(dir, "Apps", "CommonFiles", "Java");
                    }
#endif
                    if (Directory.Exists(dir))
                    {
                        javaPath = GetPath(dir);
                        if (File.Exists(javaPath))
                        {
                            goto Found;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }

            // Start searching for the portable JRE, starting in the current directory.
            try
            {
                var dir = PathEx.LocalDir;
                var drive = new DriveInfo(dir).RootDirectory.Root.Name;
                int count = 0, length = dir.Split(Path.DirectorySeparatorChar).Length;
                var subs = new[]
                {
#if x64
                    "_CommonFiles\\Java64",
                    "CommonFiles\\Java64",
#endif
                    "_CommonFiles\\Java",
                    "CommonFiles\\Java"
                };
                while (!drive.ContainsEx(dir) && ++count < length)
                {
                    foreach (var sub in subs)
                    {
                        var tmp = Path.Combine(dir, sub);
                        if (!Directory.Exists(tmp))
                        {
                            continue;
                        }
                        javaPath = GetPath(tmp);
                        if (File.Exists(javaPath))
                        {
                            goto Found;
                        }
                    }
                    dir = PathEx.Combine(dir, "..").TrimEnd(Path.DirectorySeparatorChar);
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }

            // Try getting the default path from the global JRE installation.
            try
            {
                var dirs = new[]
                {
                    "%ProgramFiles%\\Java",
#if x64
                    "%ProgramFiles(x86)%\\Java",
#endif
                    "%ProgramData%\\Oracle\\Java\\javapath"
                };
                foreach (var dir in dirs.Select(PathEx.Combine))
                {
                    if (!Directory.Exists(dir))
                    {
                        continue;
                    }
                    javaPath = GetPath(dir);
                    if (File.Exists(javaPath))
                    {
                        goto Found;
                    }
                }
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }

            // If JRE was not found.
            MessageBox.Show(Strings.JavaWarnMessage, AssemblyInfo.Title, MessageBoxButtons.OK, MessageBoxIcon.Error);
            Environment.ExitCode = 1;
            Environment.Exit(Environment.ExitCode);

            // Else
Found:
            var envPath = EnvironmentEx.GetVariablePathFull(javaPath);
            Ini.WriteDirect(nameof(JavaHandler), "Path", envPath, Attributes.ConfigPath);

            var usageDir = PathEx.Combine("%UserProfile%\\.oracle_jre_usage");
            try
            {
                if (!Directory.Exists(usageDir))
                {
                    Directory.CreateDirectory(usageDir);
                }
                DirectoryEx.SetAttributes(usageDir, FileAttributes.Hidden);
            }
            catch (Exception ex)
            {
                Log.Write(ex);
            }
            AppDomain.CurrentDomain.ProcessExit += (s, e) => DirectoryEx.TryDelete(usageDir);
        }
예제 #2
0
        internal static void RepairAppsSuiteDirs()
        {
            if (!Elevation.WritableLocation())
            {
                Elevation.RestartAsAdministrator(ActionGuid.RepairDirs);
            }

            foreach (var dirs in new[]
            {
                CorePaths.AppDirs,
                CorePaths.UserDirs
            })
            {
                foreach (var dir in dirs)
                {
                    if (!DirectoryEx.Create(dir))
                    {
                        Elevation.RestartAsAdministrator(ActionGuid.RepairDirs);
                    }
                }
            }

            var iniMap = new[]
            {
                new[]
                {
                    CorePaths.AppDirs.First(),
                    "IconResource=..\\Assets\\FolderIcons.dll,3"
                },
                new[]
                {
                    CorePaths.AppDirs.Second(),
                    "LocalizedResourceName=\"Port-Able\" - Freeware",
                    "IconResource=..\\..\\Assets\\FolderIcons.dll,4"
                },
                new[]
                {
                    CorePaths.AppDirs.Third(),
                    "LocalizedResourceName=\"PortableApps\" - Repacks",
                    "IconResource=..\\..\\Assets\\FolderIcons.dll,2"
                },
                new[]
                {
                    CorePaths.AppDirs.Last(),
                    "LocalizedResourceName=\"Custom\" - Shareware",
                    "IconResource=..\\..\\Assets\\FolderIcons.dll,1"
                },
                new[]
                {
                    PathEx.Combine(PathEx.LocalDir, "Assets"),
                    "IconResource=FolderIcons.dll,5"
                },
                new[]
                {
                    PathEx.Combine(PathEx.LocalDir, "Binaries"),
                    "IconResource=..\\Assets\\FolderIcons.dll,5"
                },
                new[]
                {
                    CorePaths.UserDirs.First(),
                    "LocalizedResourceName=Profile",
                    "IconResource=%SystemRoot%\\system32\\imageres.dll,117",
                    "IconFile=%SystemRoot%\\system32\\shell32.dll",
                    "IconIndex=-235"
                },
                new[]
                {
                    PathEx.Combine(PathEx.LocalDir, "Documents", ".cache"),
                    "IconResource=%SystemRoot%\\system32\\imageres.dll,112"
                },
                new[]
                {
                    CorePaths.UserDirs.Second(),
                    "LocalizedResourceName=@%SystemRoot%\\system32\\shell32.dll,-21770",
                    "IconResource=%SystemRoot%\\system32\\imageres.dll,-112",
                    "IconFile=%SystemRoot%\\system32\\shell32.dll",
                    "IconIndex=-235"
                },
                new[]
                {
                    CorePaths.UserDirs.Third(),
                    "LocalizedResourceName=@%SystemRoot%\\system32\\shell32.dll,-21790",
                    "IconResource=%SystemRoot%\\system32\\imageres.dll,-108",
                    "IconFile=%SystemRoot%\\system32\\shell32.dll",
                    "IconIndex=-237",
                    "InfoTip=@%SystemRoot%\\system32\\shell32.dll,-12689"
                },
                new[]
                {
                    CorePaths.UserDirs.Fourth(),
                    "LocalizedResourceName=@%SystemRoot%\\system32\\shell32.dll,-21779",
                    "IconResource=%SystemRoot%\\system32\\imageres.dll,-113",
                    "IconFile=%SystemRoot%\\system32\\shell32.dll",
                    "IconIndex=-236",
                    "InfoTip=@%SystemRoot%\\system32\\shell32.dll,-12688"
                },
                new[]
                {
                    CorePaths.UserDirs.Last(),
                    "LocalizedResourceName=@%SystemRoot%\\system32\\shell32.dll,-21791",
                    "IconResource=%SystemRoot%\\system32\\imageres.dll,-189",
                    "IconFile=%SystemRoot%\\system32\\shell32.dll",
                    "IconIndex=-238",
                    "InfoTip=@%SystemRoot%\\system32\\shell32.dll,-12690"
                },
                new[]
                {
                    PathEx.Combine(PathEx.LocalDir, "Help"),
                    "IconResource=..\\Assets\\FolderIcons.dll,4"
                }
            };

            for (var i = 0; i < iniMap.Length; i++)
            {
                var array = iniMap[i];
                var dir   = array.FirstOrDefault();
                if (!PathEx.IsValidPath(dir) || i >= iniMap.Length - 2 && !Directory.Exists(dir))
                {
                    continue;
                }
                if (!Elevation.WritableLocation(dir))
                {
                    Elevation.RestartAsAdministrator(ActionGuid.RepairDirs);
                }
                var path = PathEx.Combine(dir, "desktop.ini");
                foreach (var str in array.Skip(1))
                {
                    var ent = str?.Split('=');
                    if (ent?.Length != 2)
                    {
                        continue;
                    }
                    var key = ent.FirstOrDefault();
                    if (string.IsNullOrEmpty(key))
                    {
                        continue;
                    }
                    var val = ent.LastOrDefault();
                    if (string.IsNullOrEmpty(val))
                    {
                        continue;
                    }
                    Ini.WriteDirect(".ShellClassInfo", key, val, path);
                }
                FileEx.SetAttributes(path, FileAttributes.System | FileAttributes.Hidden);
                DirectoryEx.SetAttributes(dir, FileAttributes.ReadOnly);
            }
        }
예제 #3
0
        /// <summary>
        ///     Enables/disables the directory redirection.
        /// </summary>
        /// <param name="option">
        ///     The option that determines whether the redirect will be enabled or disabled.
        /// </param>
        /// <param name="dirMap">
        ///     The diretory map.
        ///     <para>
        ///         The key is used for the source directory, and the value is used for the
        ///         destination directory.
        ///     </para>
        /// </param>
        public static void DirRedirection(PortalizerActions option, Dictionary <string, string> dirMap)
        {
            if (dirMap?.Any() != true)
            {
                return;
            }
            var hash  = nameof(DirRedirection).Encrypt();
            var hPath = PathEx.Combine(Attributes.TempDir, hash);

            switch (option)
            {
            case PortalizerActions.Disable:
                FileEx.TryDelete(hPath);
                break;

            default:
                if (File.Exists(hPath))
                {
                    DirRedirection(PortalizerActions.Disable, dirMap);
                }
                FileEx.Create(hPath);
                break;
            }
            foreach (var data in dirMap)
            {
                if (string.IsNullOrWhiteSpace(data.Key) || string.IsNullOrWhiteSpace(data.Value))
                {
                    continue;
                }
                var srcPath  = PathEx.Combine(data.Key);
                var destPath = PathEx.Combine(data.Value);
                DirectoryEx.SetAttributes(srcPath, FileAttributes.Normal);
                var backupPath = $"{srcPath}-{{{EnvironmentEx.MachineId}}}.backup";
                switch (option)
                {
                case PortalizerActions.Disable:
                    DirectoryEx.SetAttributes(backupPath, FileAttributes.Normal);
                    if (DirectoryEx.DestroySymbolicLink(srcPath, true))
                    {
                        continue;
                    }
                    try
                    {
                        if (Directory.Exists(srcPath))
                        {
                            DirectoryEx.SetAttributes(srcPath, FileAttributes.Normal);
                            DirectoryEx.Copy(srcPath, destPath, true, true);
                            Directory.Delete(srcPath, true);
                        }
                        if (Directory.Exists(backupPath))
                        {
                            DirectoryEx.Copy(backupPath, srcPath, true, true);
                            Directory.Delete(backupPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Write(ex);
                    }
                    break;

                default:
                    if (DirectoryEx.CreateSymbolicLink(srcPath, destPath, true))
                    {
                        DirectoryEx.SetAttributes(backupPath, FileAttributes.Normal);
                        continue;
                    }
                    try
                    {
                        if (Directory.Exists(srcPath))
                        {
                            if (!Directory.Exists(backupPath))
                            {
                                DirectoryEx.Move(srcPath, backupPath);
                                DirectoryEx.SetAttributes(backupPath, FileAttributes.Hidden);
                            }
                            if (Directory.Exists(srcPath))
                            {
                                Directory.Delete(srcPath, true);
                            }
                        }
                        if (Directory.Exists(destPath))
                        {
                            DirectoryEx.SetAttributes(destPath, FileAttributes.Normal);
                            DirectoryEx.Copy(destPath, srcPath);
                        }
                        if (!Directory.Exists(srcPath))
                        {
                            Directory.CreateDirectory(srcPath);
                        }
                    }
                    catch (Exception ex)
                    {
                        Log.Write(ex);
                    }
                    break;
                }
            }
        }