using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security; namespace ScreenConnect; public static class FileSystemExtensions { public static void DemandInParentPath(string parentPath, string testPath) { NormalizeAndDemandInParentPath(parentPath, testPath); } public static string NormalizeAndDemandInParentPath(string parentPath, string testPath) { string text = TrimTrailingSeparatorsIfApplicable(Path.GetFullPath(parentPath.AssertArgumentNonNull("parentPath"))); string text2 = TrimTrailingSeparatorsIfApplicable(Path.GetFullPath(testPath.AssertArgumentNonNull("testPath"))); if (text2.Length < text.Length || !string.Equals(text, text2.Substring(0, text.Length), StringComparison.OrdinalIgnoreCase) || !string.Equals(text2, Path.Combine(text, text2.Substring(text.Length).TrimStart(new char[2] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar })), StringComparison.OrdinalIgnoreCase)) { throw new SecurityException(); } return text2; static string TrimTrailingSeparatorsIfApplicable(string path) { if (path.Length <= (Path.GetPathRoot(path)?.Length ?? 0)) { return path; } return path.TrimEnd(new char[2] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); } } public static IEnumerable GetFileSystemInfosRecursive(string path, bool includeSelf = false, bool includeHidden = false) { FileInfo fileInfo = new FileInfo(path); if (fileInfo.Exists) { return fileInfo.GetFileSystemInfosRecursive(includeSelf, includeHidden); } if (fileInfo.Attributes.AreFlagsSet(FileAttributes.Directory)) { return new DirectoryInfo(path).GetFileSystemInfosRecursive(includeSelf, includeHidden); } throw new FileNotFoundException(path); } public static IEnumerable GetFileSystemInfosRecursive(this FileSystemInfo topItem, bool includeSelf = false, bool includeHidden = false) { if (!topItem.Exists) { throw new FileNotFoundException(topItem.FullName); } return topItem.GetDescendents((FileSystemInfo parentItem) => from it in parentItem.To().GetFileSystemInfos() where includeHidden || !it.Attributes.AreAnyFlagsSet(FileAttributes.Hidden) select it, includeSelf, (FileSystemInfo it) => it is DirectoryInfo); } public static void DeleteContents(this DirectoryInfo directoryInfo) { FileInfo[] files = directoryInfo.GetFiles(); for (int i = 0; i < files.Length; i++) { files[i].Delete(); } DirectoryInfo[] directories = directoryInfo.GetDirectories(); for (int i = 0; i < directories.Length; i++) { directories[i].Delete(recursive: true); } } public static string GetAppDomainRelativePath(string? relativePath) { if (string.IsNullOrEmpty(relativePath)) { return AppDomain.CurrentDomain.BaseDirectory; } return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, relativePath); } public static string GetRelativePath(string? basePath, string fullPath, bool requireStartsWithBasePath = false) { if (basePath == null) { return fullPath; } if (!fullPath.StartsWith(basePath)) { if (requireStartsWithBasePath) { throw new InvalidOperationException(); } return fullPath; } if (fullPath.Length == basePath.Length) { return string.Empty; } int num = basePath.Length; char c = basePath[basePath.Length - 1]; if (c != Path.DirectorySeparatorChar && c != Path.AltDirectorySeparatorChar) { num++; } return fullPath.Substring(num); } public static string CombinePathsAndDemandInParent(string basePath, params string[] paths) { string text = paths.Aggregate(basePath, Path.Combine); DemandInParentPath(basePath, text); return text; } public static ToolboxItem GetToolbox(string toolboxDirectoryPath) { EnsureDirectoryExists(toolboxDirectoryPath); return GetToolbox(new DirectoryInfo(toolboxDirectoryPath)); } private static ToolboxItem GetToolbox(DirectoryInfo directory) { List list = new List(); DirectoryInfo[] directories = directory.GetDirectories(); for (int i = 0; i < directories.Length; i++) { ToolboxItem toolbox = GetToolbox(directories[i]); list.Add(toolbox); } FileInfo[] files = directory.GetFiles(); for (int i = 0; i < files.Length; i++) { ToolboxItem item = CreateToolboxItem(files[i], isDirectory: false); list.Add(item); } ToolboxItem toolboxItem = CreateToolboxItem(directory, isDirectory: true); toolboxItem.Items = list.ToArray(); return toolboxItem; } private static ToolboxItem CreateToolboxItem(FileSystemInfo fileSystemInfo, bool isDirectory) { ToolboxItem item = new ToolboxItem { Name = fileSystemInfo.Name, Attributes = (isDirectory ? ToolboxItemAttributes.Directory : ToolboxItemAttributes.None) }; Extensions.Try(delegate { item.Image = Singleton.Instance.EncodeFileIconAsImage(fileSystemInfo.FullName, isDirectory); }); return item; } public static void EnsureDirectoryExists(string directoryPath) { Directory.CreateDirectory(directoryPath); } public static void CopyFile(string sourceFilePath, string destinationFilePath, bool overwrite = false, bool ensureDestinationDirectoryExists = true) { if (ensureDestinationDirectoryExists) { EnsureDirectoryExists(Path.GetDirectoryName(destinationFilePath).AssertStateNonNull("Path.GetDirectoryName(destinationFilePath)")); } File.Copy(sourceFilePath, destinationFilePath, overwrite); } public static void DeletePath(string path) { try { Directory.Delete(path, recursive: true); } catch (IOException) { File.Delete(path); } } public static void MovePath(string fromPath, string toPath) { try { File.Move(fromPath, toPath); } catch (FileNotFoundException) { Directory.Move(fromPath, toPath); } } public static string ProcessFilePathForRunning(string filePath) { if (Path.GetExtension(filePath).ToLowerInvariant() != ".scapp") { return filePath; } string text = filePath.Substring(0, filePath.Length - ".scapp".Length); Directory.CreateDirectory(text); using (FileStream seekableStream = File.OpenRead(filePath)) { new ZipFile(seekableStream).ExtractAllEntries(text); } return (from it in Directory.GetFiles(text) orderby it select it).First(); } }