fix IsDirectory

This commit is contained in:
Aigio Liu
2021-09-04 15:17:30 +08:00
parent a703a6f5d9
commit 8d731d9d89
5 changed files with 93 additions and 92 deletions
+30 -45
View File
@@ -218,40 +218,28 @@ namespace System
}
/// <summary>
/// 判断路径是否为[文件夹(Dir)]
/// 判断路径是否为[文件夹(Dir)],返回 <see cref="FileInfo"/> 或 <see cref="DirectoryInfo"/>,<see langword="true"/> 为文件夹,<see langword="false"/> 为文件,路径不存在则为 <see langword="null"/>
/// </summary>
/// <param name="path"></param>
/// <param name="fileInfo"></param>
/// <param name="directoryInfo"></param>
/// <returns></returns>
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
}
/// <summary>
/// 获取[文件夹(Dir)]路径
/// <para>如果传入的参数为[文件(File)]路径,则返回当前所在[文件夹(Dir)]路径</para>
/// <para>如果传入的参数为[文件夹(Dir)]路径,则直接返回参数</para>
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
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
/// </summary>
/// <param name="filePath"></param>
/// <returns>单位 字节</returns>
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;
/// <summary>
/// 获取指定路径的大小
@@ -351,21 +334,23 @@ namespace System
/// <returns>单位 字节</returns>
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;
}
@@ -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";
@@ -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).");
}
}
@@ -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);
/// <summary>
/// 使用资源管理器打开某个路径
/// </summary>
/// <param name="dirPath"></param>
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)
@@ -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
/// <summary>
/// 使用资源管理器打开某个路径
/// </summary>
/// <param name="path"></param>
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!);
}
}
/// <summary>
/// 使用资源管理器打开文件夹路径
/// </summary>
/// <param name="dirPath"></param>
void OpenFolder(string dirPath);
void OpenFolderByDirectoryPath(DirectoryInfo info);
/// <summary>
/// 使用资源管理器选中文件路径
/// </summary>
/// <param name="filePath"></param>
void OpenFolderSelectFilePath(FileInfo info);
/// <summary>
/// 设置系统关闭时任务