internal static void RepairEnvironmentVariable() { if (!Elevation.IsAdministrator) { using (var process = ProcessEx.Start(PathEx.LocalPath, ActionGuid.RepairVariable, true, false)) if (process?.HasExited == false) { process.WaitForExit(); } return; } var envDir = EnvironmentEx.GetVariableValue(Settings.EnvironmentVariable); if (!Settings.DeveloperVersion && !string.IsNullOrWhiteSpace(envDir) && !envDir.EqualsEx(PathEx.LocalDir)) { SystemIntegration.Enable(true, true); } }
/// <summary> /// Sets registry values from an INI config or from a REG file. /// </summary> /// <param name="fileOrContent"> /// The path or content of an INI or REG file. /// </param> public static void SetConfig(string fileOrContent) { if (string.IsNullOrWhiteSpace(fileOrContent)) { return; } var sections = Ini.GetSections(fileOrContent, false); try { if (sections.All(x => x.StartsWith("HKEY_")) && sections.All(x => Ini.GetKeys(x, fileOrContent, false).All(y => y.Equals("@") || y.StartsWith("\"") && y.EndsWith("\"")))) { try { var regex = new Regex("%(.+?)%"); var content = File.Exists(fileOrContent) ? File.ReadAllText(fileOrContent) : fileOrContent; foreach (var variable in regex.Matches(content).Cast <Match>().Select(x => x.Value).Distinct()) { var value = EnvironmentEx.GetVariableValue(variable); if (!content.ContainsEx(variable)) { continue; } content = content.Replace(variable, value); } var cArray = TextEx.FormatNewLine(content).SplitNewLine(); Reg.ImportFile(cArray); } catch (Exception ex) { Log.Write(ex); } return; } } catch (Exception ex) { Log.Write(ex); } foreach (var section in Ini.GetSections(fileOrContent, false)) { var key = Ini.Read(section, "Key", default(string), fileOrContent); if (string.IsNullOrEmpty(key)) { continue; } var entry = Ini.Read(section, "Entry", default(string), fileOrContent); var value = Ini.Read(section, "Value", default(string), fileOrContent); if (string.IsNullOrEmpty(value)) { continue; } if (!string.IsNullOrEmpty(entry)) { entry = PathEx.Combine(entry); } value = PathEx.Combine(value); var kind = Ini.Read(section, "Kind", default(string), fileOrContent); if (string.IsNullOrEmpty(kind)) { continue; } try { Reg.Write(key, !string.IsNullOrWhiteSpace(entry) ? entry : null, value, (RegistryValueKind)Enum.Parse(typeof(RegistryValueKind), kind)); } catch (Exception ex) { Log.Write(ex); continue; } var temp = Ini.Read(section, "Temp", default(string), fileOrContent); if (!temp.EqualsEx("True", "Entry")) { continue; } AppDomain.CurrentDomain.ProcessExit += (s, e) => { switch (temp.ToLower()) { case "true": Reg.RemoveSubKey(key); break; case "entry": Reg.RemoveEntry(key, entry); break; } }; } }
internal static bool AppsSuiteIsHealthy(bool repair = true) { if (!Elevation.WritableLocation()) { Elevation.RestartAsAdministrator(); } VersionValidation(); while (true) { try { if (!File.Exists(CorePaths.AppsDownloader) || !File.Exists(CorePaths.AppsSuiteUpdater) || !File.Exists(CorePaths.FileArchiver)) { throw new FileNotFoundException(); } } catch (FileNotFoundException ex) { Log.Write(ex); if (!repair) { return(false); } RepairAppsSuite(); } try { foreach (var dir in CorePaths.AppDirs.Where(dir => !Directory.Exists(dir))) { throw new PathNotFoundException(dir); } } catch (PathNotFoundException ex) { Log.Write(ex); if (!repair) { return(false); } RepairAppsSuiteDirs(); } try { var envDir = EnvironmentEx.GetVariableValue(Settings.EnvironmentVariable); if (!Settings.DeveloperVersion && !string.IsNullOrWhiteSpace(envDir) && !envDir.EqualsEx(PathEx.LocalDir)) { throw new ArgumentInvalidException(nameof(envDir)); } } catch (ArgumentInvalidException ex) { Log.Write(ex); if (!repair) { return(false); } RepairEnvironmentVariable(); } if (!repair) { return(true); } repair = false; } }
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); }
private static void Main() { Log.AllowLogging(); #if x86 var curPath64 = PathEx.Combine(PathEx.LocalDir, "NTLite64Portable.exe"); if (Environment.Is64BitOperatingSystem && File.Exists(curPath64)) { ProcessEx.Start(curPath64, EnvironmentEx.CommandLine(false)); return; } var appDir = PathEx.Combine(PathEx.LocalDir, "App\\NTLite"); var updaterPath = PathEx.Combine(appDir, "NTLiteUpdater.exe"); #else var appDir = PathEx.Combine(PathEx.LocalDir, "App\\NTLite64"); var updaterPath = PathEx.Combine(appDir, "NTLiteUpdater64.exe"); #endif var appPath = Path.Combine(appDir, "NTLite.exe"); if (!File.Exists(updaterPath) || ProcessEx.IsRunning(Path.GetFileNameWithoutExtension(appPath)) || ProcessEx.IsRunning(Path.GetFileNameWithoutExtension(updaterPath))) { return; } using (new Mutex(true, ProcessEx.CurrentName, out bool newInstance)) { if (!newInstance) { return; } var dataDir = PathEx.Combine(PathEx.LocalDir, "Data"); try { var temp = Path.Combine(dataDir, "TEMP"); if (!Directory.Exists(temp)) { Directory.CreateDirectory(temp); } var cfgPath = Path.Combine(dataDir, "settings.xml"); if (File.Exists(cfgPath)) { var match = Regex.Match(File.ReadAllText(cfgPath), "<TempFolder>(.+?)</TempFolder>", RegexOptions.IgnoreCase).Groups[1].Value; if (!string.IsNullOrWhiteSpace(match)) { Encoding encoding; var sb = new StringBuilder(); using (var sr = new StreamReader(cfgPath)) { string line; encoding = sr.CurrentEncoding; while ((line = sr.ReadLine()) != null) { var m = Regex.Match(line, "<TempFolder>(.+?)</TempFolder>", RegexOptions.IgnoreCase).Groups[1].Value; if (!string.IsNullOrWhiteSpace(m)) { var dirName = Path.GetFileName(EnvironmentEx.GetVariableValue("CurDir")); if (dirName != null) { line = line.Replace(m, line.ToLower().Contains($"{dirName.ToLower()}\\data\\temp") ? temp : "%TEMP%"); } } sb.AppendLine(line); } } using (var sw = new StreamWriter(cfgPath, false, encoding)) sw.Write(sb.ToString()); } } else { var content = Resources.DefaultSetting; content = content.Replace("%TEMP%", temp); using (var sw = File.CreateText(cfgPath)) sw.Write(content); } } catch (Exception ex) { Log.Write(ex); } try { var srcDir = PathEx.Combine(appDir, "Presets"); if (Directory.Exists(srcDir) && !Data.DirIsLink(srcDir)) { var destDir = PathEx.Combine(dataDir, "Presets"); Data.DirCopy(srcDir, destDir, true, true); Directory.Delete(srcDir, true); } } catch (Exception ex) { Log.Write(ex); } var dirMap = new Dictionary <string, string> { { PathEx.Combine(appDir, "Presets"), PathEx.Combine(dataDir, "Presets") } }; var fileMap = new Dictionary <string, string> { { Path.Combine(appDir, "settings.xml"), Path.Combine(dataDir, "settings.xml") }, { Path.Combine(appDir, "settingsUI.xml"), Path.Combine(dataDir, "settingsUI.xml") } }; Helper.ApplicationStart(updaterPath, "/silent", null); if (!File.Exists(appPath)) { var updIniPath = Path.ChangeExtension(updaterPath, ".ini"); if (!string.IsNullOrEmpty(updIniPath) && File.Exists(updIniPath)) { File.Delete(updIniPath); } return; } Helper.DirectoryForwarding(Helper.Options.Start, dirMap); Helper.FileForwarding(Helper.Options.Start, fileMap, true); Helper.ApplicationStart(appPath, EnvironmentEx.CommandLine(false), false); Helper.DirectoryForwarding(Helper.Options.Exit, dirMap); Helper.FileForwarding(Helper.Options.Exit, fileMap, true); } }