This repository was archived by the owner on Jan 25, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathFiles.cs
More file actions
117 lines (104 loc) · 4.8 KB
/
Copy pathFiles.cs
File metadata and controls
117 lines (104 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.IO;
using System.Security;
using PCL.Core.Logging;
namespace PCL.Core.IO;
public static class Files
{
/// <summary>
/// 在指定路径创建一个指向目标文件的 .lnk 快捷方式。
/// </summary>
/// <param name="shortcut">要创建的快捷方式完整路径,建议以 ".lnk" 结尾</param>
/// <param name="target">被指向的目标文件或可执行程序路径</param>
/// <param name="arguments">启动时的命令行参数</param>
/// <param name="workingDirectory">快捷方式的起始目录</param>
/// <param name="description">快捷方式说明</param>
/// <param name="icon">自定义图标,格式 "图标文件路径,索引"</param>
// Partly generated by o4-mini-high (20250719)
public static void CreateShortcut(
string shortcut,
string target,
string? arguments = null,
string? workingDirectory = null,
string? description = null,
string? icon = null)
{
if (string.IsNullOrWhiteSpace(shortcut))
throw new ArgumentException("shortcutPath 不能为空", nameof(shortcut));
if (string.IsNullOrWhiteSpace(target))
throw new ArgumentException("targetFilePath 不能为空", nameof(target));
// 确保目录存在
var dir = Path.GetDirectoryName(shortcut);
if (!string.IsNullOrEmpty(dir) && !Directory.Exists(dir)) Directory.CreateDirectory(dir);
// 与 WshShell 交互
var shellType = Type.GetTypeFromProgID("WScript.Shell", throwOnError: true)!;
dynamic shell = Activator.CreateInstance(shellType)!;
var link = shell.CreateShortcut(shortcut)!;
// 设置属性
link.TargetPath = target;
if (!string.IsNullOrEmpty(arguments)) link.Arguments = arguments;
if (!string.IsNullOrEmpty(workingDirectory)) link.WorkingDirectory = workingDirectory;
else link.WorkingDirectory = Path.GetDirectoryName(target) ?? Path.GetPathRoot(target);
if (!string.IsNullOrEmpty(description)) link.Description = description;
if (!string.IsNullOrEmpty(icon)) link.IconLocation = icon;
// 保存 .lnk 文件
link.Save();
}
/// <summary>
/// 检查是否拥有对指定文件夹的 I/O 权限。
/// 如果文件夹不存在,会返回 false。
/// </summary>
/// <param name="path">要检查的文件夹路径。</param>
/// <returns>如果拥有权限且文件夹存在,则为 true;否则为 false。</returns>
public static bool CheckPermission(string path) {
try {
if (string.IsNullOrWhiteSpace(path)) {
return false;
}
// 检查一些系统特殊文件夹,这些文件夹通常没有权限
if (path.EndsWith(":\\System Volume Information", StringComparison.OrdinalIgnoreCase) ||
path.EndsWith(":\\$RECYCLE.BIN", StringComparison.OrdinalIgnoreCase)) {
return false;
}
// 检查文件夹是否存在
if (!Directory.Exists(path)) {
return false;
}
// 核心逻辑:通过创建和删除临时文件来检查权限
var tempFileName = Path.Combine(path, Guid.NewGuid().ToString());
using (File.Create(tempFileName)) { }
File.Delete(tempFileName);
return true;
} catch (IOException) {
return false;
} catch (UnauthorizedAccessException) {
return false;
} catch (SecurityException) {
return false;
} catch (Exception ex) {
// 捕获并记录其他未知异常
LogWrapper.Warn(ex, $"没有对文件夹 {path} 的权限,请尝试以管理员权限运行。");
return false;
}
}
/// <summary>
/// 检查是否拥有对指定文件夹的 I/O 权限。
/// 如果出错,则抛出异常。
/// </summary>
/// <param name="path">要检查的文件夹路径。</param>
/// <exception cref="ArgumentNullException">文件夹路径为空或只包含空格。</exception>
/// <exception cref="DirectoryNotFoundException">文件夹不存在。</exception>
/// <exception cref="UnauthorizedAccessException">没有访问文件夹的权限。</exception>
public static void CheckPermissionWithException(string path) {
if (string.IsNullOrWhiteSpace(path)) {
throw new ArgumentNullException(nameof(path), "文件夹名不能为空!");
}
if (!Directory.Exists(path)) {
throw new DirectoryNotFoundException("文件夹不存在!");
}
// 核心逻辑:创建和删除临时文件
var tempFileName = Path.Combine(path, "CheckPermission");
using (File.Create(tempFileName)) { }
File.Delete(tempFileName);
}
}