From 8d731d9d89f21885e9644cd2bc531b2d61394bd3 Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Sat, 4 Sep 2021 15:17:30 +0800 Subject: [PATCH] fix IsDirectory --- src/Common.CoreLib/IOPath.cs | 75 ++++++++----------- .../LinuxDesktopPlatformServiceImpl.cs | 20 +++-- .../MacDesktopPlatformServiceImpl.cs | 27 ++++--- .../WindowsDesktopPlatformServiceImpl.cs | 36 ++++----- .../Services/IDesktopPlatformService.cs | 27 ++++++- 5 files changed, 93 insertions(+), 92 deletions(-) diff --git a/src/Common.CoreLib/IOPath.cs b/src/Common.CoreLib/IOPath.cs index e92982d74..a76d62d46 100644 --- a/src/Common.CoreLib/IOPath.cs +++ b/src/Common.CoreLib/IOPath.cs @@ -218,40 +218,28 @@ namespace System } /// - /// 判断路径是否为[文件夹(Dir)] + /// 判断路径是否为[文件夹(Dir)],返回 或 , 为文件夹, 为文件,路径不存在则为 /// /// + /// + /// /// - public static bool IsDirectory(string path) + public static bool? IsDirectory(string path, [NotNullWhen(false)] out FileInfo? fileInfo, [NotNullWhen(true)] out DirectoryInfo? directoryInfo) { -#if NET35 - throw new PlatformNotSupportedException(); -#else - var attr = File.GetAttributes(path); - return attr.HasFlag(FileAttributes.Directory); -#endif - } - - /// - /// 获取[文件夹(Dir)]路径 - /// 如果传入的参数为[文件(File)]路径,则返回当前所在[文件夹(Dir)]路径 - /// 如果传入的参数为[文件夹(Dir)]路径,则直接返回参数 - /// - /// - /// - public static string GetDirectoryPath(string path) - { - try + fileInfo = new(path); + if (fileInfo.Exists) { - if (!IsDirectory(path)) - { - return Path.GetDirectoryName(path) ?? string.Empty; - } + directoryInfo = null; + return false; } - catch + fileInfo = null; + directoryInfo = new(path); + if (directoryInfo.Exists) { + return true; } - return path; + directoryInfo = null; + return false; } static FileStream OpenReadCore(string filePath) => new(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete); @@ -337,12 +325,7 @@ namespace System /// /// /// 单位 字节 - public static double GetFileSize(string filePath) - { - // 定义一个FileInfo对象,是指与filePath所指向的文件相关联,以获取其大小 - FileInfo fileInfo = new(filePath); - return fileInfo.Length; - } + public static double GetFileSize(FileInfo fileInfo) => fileInfo.Exists ? fileInfo.Length : 0D; /// /// 获取指定路径的大小 @@ -351,21 +334,23 @@ namespace System /// 单位 字节 public static double GetDirectorySize(string dirPath) { - double len = 0; + double len = 0D; // 判断该路径是否存在(是否为文件夹) - if (!IsDirectory(dirPath)) + var isDirectory = IsDirectory(dirPath, out var fileInfo, out var directoryInfo); + if (isDirectory.HasValue) { - //查询文件的大小 - len = GetFileSize(dirPath); - } - else - { - // 定义一个DirectoryInfo对象 - DirectoryInfo di = new(dirPath); - // 通过GetFiles方法,获取di目录中的所有文件的大小 - len += di.GetFiles().Sum(x => x.Length); - // 获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归 - len += di.GetDirectories().Sum(x => GetDirectorySize(x.FullName)); + if (!isDirectory.Value) + { + //查询文件的大小 + len = GetFileSize(fileInfo!); + } + else + { + // 通过GetFiles方法,获取di目录中的所有文件的大小 + len += directoryInfo!.GetFiles().Sum(x => x.Length); + // 获取di中所有的文件夹,并存到一个新的对象数组中,以进行递归 + len += directoryInfo.GetDirectories().Sum(x => GetDirectorySize(x.FullName)); + } } return len; } diff --git a/src/ST.Client.Desktop.Linux/Services/Implementation/LinuxDesktopPlatformServiceImpl.cs b/src/ST.Client.Desktop.Linux/Services/Implementation/LinuxDesktopPlatformServiceImpl.cs index 0de793d7d..0200c3959 100644 --- a/src/ST.Client.Desktop.Linux/Services/Implementation/LinuxDesktopPlatformServiceImpl.cs +++ b/src/ST.Client.Desktop.Linux/Services/Implementation/LinuxDesktopPlatformServiceImpl.cs @@ -72,18 +72,16 @@ namespace System.Application.Services.Implementation } public const string xdg = "xdg-open"; - public void OpenFolder(string dirPath) + + public void OpenFolderByDirectoryPath(DirectoryInfo info) { - if (IOPath.IsDirectory(dirPath)) - { - IDesktopPlatformService.Instance.StartProcess(xdg, dirPath); - } - else - { - var path = new FileInfo(dirPath); - if (!string.IsNullOrWhiteSpace(path.DirectoryName)) - IDesktopPlatformService.Instance.StartProcess(xdg, path.DirectoryName); - } + IDesktopPlatformService.Instance.StartProcess(xdg, info.FullName); + } + + public void OpenFolderSelectFilePath(FileInfo info) + { + if (info.DirectoryName == null) return; + IDesktopPlatformService.Instance.StartProcess(xdg, info.DirectoryName); } public const string kate = "kate"; diff --git a/src/ST.Client.Desktop.Mac/Services/Implementation/MacDesktopPlatformServiceImpl.cs b/src/ST.Client.Desktop.Mac/Services/Implementation/MacDesktopPlatformServiceImpl.cs index 0a56ca328..d48ec1554 100644 --- a/src/ST.Client.Desktop.Mac/Services/Implementation/MacDesktopPlatformServiceImpl.cs +++ b/src/ST.Client.Desktop.Mac/Services/Implementation/MacDesktopPlatformServiceImpl.cs @@ -127,16 +127,14 @@ namespace System.Application.Services.Implementation } - public void OpenFolder(string dirPath) + public void OpenFolderByDirectoryPath(DirectoryInfo info) { - if (IOPath.IsDirectory(dirPath)) - { - NSWorkspace.SharedWorkspace.SelectFile(null, dirPath); - } - else - { - NSWorkspace.SharedWorkspace.ActivateFileViewer(new[] { new NSUrl(dirPath, isDir: false) }); - } + NSWorkspace.SharedWorkspace.SelectFile(null, info.FullName); + } + + public void OpenFolderSelectFilePath(FileInfo info) + { + NSWorkspace.SharedWorkspace.ActivateFileViewer(new[] { new NSUrl(info.FullName, isDir: false) }); } const string TextEdit = "TextEdit"; @@ -171,7 +169,8 @@ namespace System.Application.Services.Implementation } public string GetLastSteamLoginUserName() => string.Empty; - public string? GetRegistryVdfPath() { + public string? GetRegistryVdfPath() + { var value = string.Format( "{0}Users{0}{1}{0}Library{0}Application Support{0}Steam{0}registry.vdf", Path.DirectorySeparatorChar, @@ -191,18 +190,18 @@ namespace System.Application.Services.Implementation { var oldStr = $"\t\t\t\t\t\"AutoLoginUser\"\t\t\"{autoLoginUser}\"\n"; var newStr = $"\t\t\t\t\t\"AutoLoginUser\"\t\t\"{userName}\"\n"; - VdfHelper.UpdateValueByReplaceNoPattern(registryVdfPath, oldStr, newStr); + VdfHelper.UpdateValueByReplaceNoPattern(registryVdfPath, oldStr, newStr); } else { - Log.Error("SetCurrentUser","UpdateAuthorizedAutoLoginUser Fail(0). AutoLoginUser IsNull"); + Log.Error("SetCurrentUser", "UpdateAuthorizedAutoLoginUser Fail(0). AutoLoginUser IsNull"); } } } catch (Exception e) { - Log.Error("SetCurrentUser", e, "UpdateAuthorizedAutoLoginUser Fail(0)."); - } + Log.Error("SetCurrentUser", e, "UpdateAuthorizedAutoLoginUser Fail(0)."); + } } diff --git a/src/ST.Client.Desktop.Windows/Services/Implementation/WindowsDesktopPlatformServiceImpl.cs b/src/ST.Client.Desktop.Windows/Services/Implementation/WindowsDesktopPlatformServiceImpl.cs index d973e7e4c..628f25a4a 100644 --- a/src/ST.Client.Desktop.Windows/Services/Implementation/WindowsDesktopPlatformServiceImpl.cs +++ b/src/ST.Client.Desktop.Windows/Services/Implementation/WindowsDesktopPlatformServiceImpl.cs @@ -69,30 +69,24 @@ namespace System.Application.Services.Implementation const string explorer = "explorer.exe"; static string Explorer => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), explorer); - /// - /// 使用资源管理器打开某个路径 - /// - /// - public void OpenFolder(string dirPath) + public void OpenFolderByDirectoryPath(DirectoryInfo info) { - if (IOPath.IsDirectory(dirPath)) + Process.Start(new ProcessStartInfo { - Process.Start(new ProcessStartInfo - { - FileName = Explorer, - Arguments = $"\"{dirPath}\"", - UseShellExecute = false, - }); - } - else + FileName = Explorer, + Arguments = $"\"{info.FullName}\"", + UseShellExecute = false, + }); + } + + public void OpenFolderSelectFilePath(FileInfo info) + { + Process.Start(new ProcessStartInfo { - Process.Start(new ProcessStartInfo - { - FileName = Explorer, - Arguments = $"/select,\"{dirPath}\"", - UseShellExecute = false, - }); - } + FileName = Explorer, + Arguments = $"/select,\"{info.FullName}\"", + UseShellExecute = false, + }); } public string? GetFileName(TextReaderProvider provider) diff --git a/src/ST.Client.Desktop/Services/IDesktopPlatformService.cs b/src/ST.Client.Desktop/Services/IDesktopPlatformService.cs index c9f45ccd5..5ab886df3 100644 --- a/src/ST.Client.Desktop/Services/IDesktopPlatformService.cs +++ b/src/ST.Client.Desktop/Services/IDesktopPlatformService.cs @@ -2,6 +2,7 @@ using System.Application.Models; using System.Application.Models.Settings; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Runtime.Versioning; using System.Security.Cryptography; using System.Windows; @@ -138,8 +139,32 @@ namespace System.Application.Services /// /// 使用资源管理器打开某个路径 /// + /// + void OpenFolder(string path) + { + var isDirectory = IOPath.IsDirectory(path, out var fileInfo, out var directoryInfo); + if (!isDirectory.HasValue) return; + if (isDirectory.Value) + { + OpenFolderByDirectoryPath(directoryInfo!); + } + else + { + OpenFolderSelectFilePath(fileInfo!); + } + } + + /// + /// 使用资源管理器打开文件夹路径 + /// /// - void OpenFolder(string dirPath); + void OpenFolderByDirectoryPath(DirectoryInfo info); + + /// + /// 使用资源管理器选中文件路径 + /// + /// + void OpenFolderSelectFilePath(FileInfo info); /// /// 设置系统关闭时任务