c# 如何创建快捷方式并获取现有快捷方式的路径(最佳实践)
c# How to create a Shortcut and get Path of an existing Shortcut (Best Practice)
一个项目搜索多个快捷方式以找到它们的路径。它还创建快捷方式。
阅读许多 post 之后,似乎有多种方法可以解决这个问题,有些使用参考:COM -> Windows 脚本主机对象模型,有些则没有。使用需要添加此引用的选项是否会造成性能负担?
我找到了一个 post,它展示了如何使用旧的 VB 代码来创建快捷方式,并在下面 posted 以防它对任何人有帮助,并询问这种方式是否以某种方式比使用参考更小的性能压力:Windows脚本宿主对象模型
string[] myLines = {"set WshShell = WScript.CreateObject(\"WScript.Shell\")",
"strDesktop = \"" + destinationDir + "\"",
"set oShellLink = WshShell.CreateShortcut(\"" + path2shortcut + "\")"
"oShellLink.TargetPath = \"" + targetPath + "\"",
"oShellLink.WindowStyle = 1",
"oShellLink.HotKey = \"CTRL+SHIFT+F\"",
"oShellLink.IconLocation = \"notepad.exe, 0\"",
"oShellLink.WorkingDirectory = strDesktop",
"oShellLink.Save()" };
System.IO.File.WriteAllLines("test.vbs", myLines);
System.Diagnostics.Process P = System.Diagnostics.Process.Start("test.vbs");
P.WaitForExit(int.MaxValue);
System.IO.File.Delete("test.vbs");
因为上面不需要添加引用,Windows脚本主机对象模型,
我想知道使用一种方法来获取快捷方式的路径是否对性能更好,这也不需要对 Windows 脚本主机对象模型的引用。
这里有 2 个搜索快捷方式的选项。
选项 1) 使用对 COM 的引用 -> Windows 脚本主机对象模型..
WshShell shell = new WshShell();
link = (IWshShortcut)shell.CreateShortcut(linkPathName);
MessageBox.Show(link.TargetPath);
选项 2) 不使用引用,它使用 FileStream,名为 Blez 的用户展示了如何在不添加引用的情况下进行操作
[这里是link-https://blez.wordpress.com/2013/02/18/get-file-shortcuts-target-with-c/]
FileStream fileStream = File.Open(file, FileMode.Open, FileAccess.Read)
using (System.IO.BinaryReader fileReader = new BinaryReader(fileStream))
{
fileStream.Seek(0x14, SeekOrigin.Begin); // Seek to flags
uint flags = fileReader.ReadUInt32(); // Read flags
// ... code continues for another 15 lines
}
如果一次迭代许多快捷方式(大约 100 个),这些选项中的任何一个是否对性能更好?
我不确定这对性能造成的负担,所以我认为使用 'using' 语句也许是明智的? (也许这是不可能的,或者 'overkill')我已经尝试了很多方法,但没有找到一种方法来做到这一点。
我什至尝试直接引用 DLL:
'[System.Runtime.InteropServices.DllImport("shell32.dll")]'
仍然没有运气。
所以我请你帮忙找到创建快捷方式和搜索快捷方式路径的最佳性能。
欢迎任何意见。我尽量做到简单和具体。非常感谢您的帮助!
以下是对您问题的部分回答:
据我所知,您是创建 快捷方式文件的唯一方法。
这是我遇到的读取快捷方式文件的目标路径最快的方法。这也使用 Shell32 参考。
public static string GetShortcutTargetFile(string shortcutFilename)
{
string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
Shell shell = new Shell();
Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
return link.Path;
}
return string.Empty;
}
我已经测试过它可以读取 100 个快捷方式文件的目标路径。对于我的机器,它在 0.9 秒内读取这些路径。
一个项目搜索多个快捷方式以找到它们的路径。它还创建快捷方式。
阅读许多 post 之后,似乎有多种方法可以解决这个问题,有些使用参考:COM -> Windows 脚本主机对象模型,有些则没有。使用需要添加此引用的选项是否会造成性能负担?
我找到了一个 post,它展示了如何使用旧的 VB 代码来创建快捷方式,并在下面 posted 以防它对任何人有帮助,并询问这种方式是否以某种方式比使用参考更小的性能压力:Windows脚本宿主对象模型
string[] myLines = {"set WshShell = WScript.CreateObject(\"WScript.Shell\")",
"strDesktop = \"" + destinationDir + "\"",
"set oShellLink = WshShell.CreateShortcut(\"" + path2shortcut + "\")"
"oShellLink.TargetPath = \"" + targetPath + "\"",
"oShellLink.WindowStyle = 1",
"oShellLink.HotKey = \"CTRL+SHIFT+F\"",
"oShellLink.IconLocation = \"notepad.exe, 0\"",
"oShellLink.WorkingDirectory = strDesktop",
"oShellLink.Save()" };
System.IO.File.WriteAllLines("test.vbs", myLines);
System.Diagnostics.Process P = System.Diagnostics.Process.Start("test.vbs");
P.WaitForExit(int.MaxValue);
System.IO.File.Delete("test.vbs");
因为上面不需要添加引用,Windows脚本主机对象模型, 我想知道使用一种方法来获取快捷方式的路径是否对性能更好,这也不需要对 Windows 脚本主机对象模型的引用。
这里有 2 个搜索快捷方式的选项。
选项 1) 使用对 COM 的引用 -> Windows 脚本主机对象模型..
WshShell shell = new WshShell();
link = (IWshShortcut)shell.CreateShortcut(linkPathName);
MessageBox.Show(link.TargetPath);
选项 2) 不使用引用,它使用 FileStream,名为 Blez 的用户展示了如何在不添加引用的情况下进行操作 [这里是link-https://blez.wordpress.com/2013/02/18/get-file-shortcuts-target-with-c/]
FileStream fileStream = File.Open(file, FileMode.Open, FileAccess.Read)
using (System.IO.BinaryReader fileReader = new BinaryReader(fileStream))
{
fileStream.Seek(0x14, SeekOrigin.Begin); // Seek to flags
uint flags = fileReader.ReadUInt32(); // Read flags
// ... code continues for another 15 lines
}
如果一次迭代许多快捷方式(大约 100 个),这些选项中的任何一个是否对性能更好? 我不确定这对性能造成的负担,所以我认为使用 'using' 语句也许是明智的? (也许这是不可能的,或者 'overkill')我已经尝试了很多方法,但没有找到一种方法来做到这一点。 我什至尝试直接引用 DLL: '[System.Runtime.InteropServices.DllImport("shell32.dll")]' 仍然没有运气。
所以我请你帮忙找到创建快捷方式和搜索快捷方式路径的最佳性能。
欢迎任何意见。我尽量做到简单和具体。非常感谢您的帮助!
以下是对您问题的部分回答:
据我所知,您是创建 快捷方式文件的唯一方法。 这是我遇到的读取快捷方式文件的目标路径最快的方法。这也使用 Shell32 参考。
public static string GetShortcutTargetFile(string shortcutFilename)
{
string pathOnly = System.IO.Path.GetDirectoryName(shortcutFilename);
string filenameOnly = System.IO.Path.GetFileName(shortcutFilename);
Shell shell = new Shell();
Folder folder = shell.NameSpace(pathOnly);
FolderItem folderItem = folder.ParseName(filenameOnly);
if (folderItem != null)
{
Shell32.ShellLinkObject link = (Shell32.ShellLinkObject)folderItem.GetLink;
return link.Path;
}
return string.Empty;
}
我已经测试过它可以读取 100 个快捷方式文件的目标路径。对于我的机器,它在 0.9 秒内读取这些路径。