gpt4 book ai didi

c# - 使用 C# 通过 targetpath 删除/修改快捷方式

转载 作者:太空宇宙 更新时间:2023-11-03 13:58:06 27 4
gpt4 key购买 nike

我们有用户将他们桌面上的快捷方式文件重命名为我们的应用程序。如果应用程序的图标发生变化,基于目标路径删除/修改快捷方式的最佳方法是什么?换句话说,我很难找到文件名,因为它一直在变化。

最佳答案

这是一个很好的问题,我很惊讶在 10 年内没有人回答正确。

下面的代码遍历给定文件夹中的所有链接,找到一个 TargetPath 指向当前正在执行的 WinForms 应用程序的链接。

将对项目的 COM 引用添加到 Windows 脚本宿主对象模型

    using IWshRuntimeLibrary;

private static void DeleteShortcuts(string path)
{
// Example for path: Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);

foreach (var fileName in Directory.GetFiles(path, "*.lnk"))
{
WshShell shell = new WshShell();
IWshShortcut link = (IWshShortcut)shell.CreateShortcut(fileName);
if (link.TargetPath == Application.ExecutablePath)
{
System.IO.File.Delete(fileName);
}
}
}

注意: Application.ExecutablePath 适用于 WinForms 以获取当前的 exe 路径,对于控制台应用程序,您应该使用 Path.GetDirectoryName(Environment.GetCommandLineArgs() [0])。请参阅其他类型项目的文档。

关于c# - 使用 C# 通过 targetpath 删除/修改快捷方式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11512048/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com