-
Notifications
You must be signed in to change notification settings - Fork 29
/
File.cs
85 lines (82 loc) · 3.13 KB
/
File.cs
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
using IWshRuntimeLibrary;
using System.Diagnostics;
namespace File_cs
{
public class Shortcut
{
/// <summary>
/// 获取快捷方式目标路径
/// </summary>
/// <param name="Shortcut_Path">快捷方式路径</param>
/// <returns>成功返回 快捷方式目标路径,失败返回 null</returns>
public static string Get_Shortcut_TargetPath(string Shortcut_Path)
{
if (System.IO.File.Exists(Shortcut_Path))
{
WshShell Shell = new WshShell();
IWshShortcut Shortcut = (IWshShortcut)Shell.CreateShortcut(Shortcut_Path);
return Shortcut.TargetPath;
}
return null;
}
/// <summary>
/// 从快捷方式数组中匹配目标路径
/// </summary>
/// <param name="Shortcut_Path">快捷方式路径,字符串数组</param>
/// <param name="Path">目标路径</param>
/// <returns>成功返回 匹配目标路径的快捷方式路径,失败返回 null</returns>
public static string Get_Shortcut_TargetPath_Array(string[] Shortcut_Path, string Path)
{
if (Shortcut_Path.Length > 0)
{
foreach (string Temp_Shortcut_Path in Shortcut_Path)
{
//Debug.Print(Temp_Shortcut_Path);
if (Get_Shortcut_TargetPath(Temp_Shortcut_Path) == Path)
{
return Temp_Shortcut_Path;
}
}
}
return null;
}
/// <summary>
/// 创建快捷方式
/// </summary>
/// <param name="LinkPath">快捷方式路径</param>
/// <param name="TargetPath">目标路径</param>
/// <param name="IconPath">图标路径,为空时使用目标程序的图标</param>
public static void Create_Shortcut(string LinkPath, string TargetPath, string IconPath = "")
{
WshShell shell = new WshShell();
IWshShortcut Shortcut = (IWshShortcut)shell.CreateShortcut(LinkPath);
Shortcut.TargetPath = TargetPath;
if (IconPath != "")
{
Shortcut.IconLocation = IconPath;
}
Shortcut.Save();
}
}
public class File
{
/// <summary>
/// 枚举文件
/// </summary>
/// <param name="Path">欲寻找的路径</param>
/// <param name="FileName">欲寻找的文件名,支持通配符,例:*.lnk</param>
/// <param name="Traversal">是否遍历子目录</param>
/// <returns>返回 找到的文件数组(完整路径)</returns>
public static string[] File_Enumeration(string Path, string FileName, bool Traversal)
{
if (Traversal == true)
{
return System.IO.Directory.GetFiles(Path, FileName, System.IO.SearchOption.AllDirectories);
}
else
{
return System.IO.Directory.GetFiles(Path, FileName, System.IO.SearchOption.TopDirectoryOnly);
}
}
}
}