代码之家  ›  专栏  ›  技术社区  ›  user766595

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

  •  3
  • user766595  · 技术社区  · 13 年前

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

    4 回复  |  直到 13 年前
        1
  •  1
  •   Mike Keskinov    4 年前

    这是一个很好的问题,我很惊讶10年来没有人答对。

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

    将对项目的COM引用添加到 Windows Script Host Object Model

        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路径,适用于您应该使用的Console应用程序 Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) 。有关其他类型的项目,请参阅文档。

        2
  •  0
  •   Omar    13 年前

    你应该使用 FileSystemWatcher 类别:

    当 目录或目录中的文件发生更改。

    你可以利用的事实 FileSystemWatcher.Changed , FileSystemWatcher.Created , FileSystemWatcher.Renamed , FileSystemWatcher.Deleted 事件以保持对文件的控制。

    下面是MSDN的一个示例:

    public static void Main()
    {
        // Create a new FileSystemWatcher and set its properties.
        FileSystemWatcher watcher = new FileSystemWatcher();
        watcher.Path = "mypath";
        /* Watch for changes in LastAccess and LastWrite times, and
           the renaming of files or directories. */
        watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
           | NotifyFilters.FileName | NotifyFilters.DirectoryName;
        // Only watch text files.
        watcher.Filter = "*.txt";
    
        // Add event handlers.
        watcher.Changed += new FileSystemEventHandler(OnChanged);
        watcher.Created += new FileSystemEventHandler(OnChanged);
        watcher.Deleted += new FileSystemEventHandler(OnChanged);
        watcher.Renamed += new RenamedEventHandler(OnRenamed);
    
        // Begin watching.
        watcher.EnableRaisingEvents = true;
    
        // Wait for the user to quit the program.
        Console.WriteLine("Press \'q\' to quit the sample.");
        while(Console.Read()!='q');
    }
    
    // Define the event handlers.
    private static void OnChanged(object source, FileSystemEventArgs e)
    {
        // Specify what is done when a file is changed, created, or deleted.
       Console.WriteLine("File: " +  e.FullPath + " " + e.ChangeType);
    }
    
    private static void OnRenamed(object source, RenamedEventArgs e)
    {
        // Specify what is done when a file is renamed.
        Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
    }
    
        3
  •  0
  •   Chibueze Opata    13 年前

    重命名快捷方式不会修改目标路径,但是,我知道在c#中使用快捷方式的最好方法是使用 IwshRuntimeLibrary

        4
  •  -1
  •   HatSoft    13 年前

    要删除文件,请使用 System.IO.File.Delete 方法

    要修改可以使用的文件 System.IO.File.AppendText 方法

    根据以下评论进行更新:

    请使用ShellClass创建或修改快捷方式 需要使用从桌面获取特殊目录 环境.专用文件夹.桌面目录

    这里可以找到一个非常好的逐步展示的例子 http://www.codeproject.com/Articles/146757/Add-Remove-Startup-Folder-Shortcut-to-Your-App