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

时间:2012-07-16 20:31:08

标签: c# shortcut

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

3 个答案:

答案 0 :(得分:1)

您应该使用FileSystemWatcher class:

  

收听文件系统更改通知并在何时引发事件   目录或目录中的文件发生更改。

事实上,您可以利用FileSystemWatcher.ChangedFileSystemWatcher.CreatedFileSystemWatcher.RenamedFileSystemWatcher.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);
}

答案 1 :(得分:0)

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

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

  

从以下评论后更新:

     

请使用ShellClass创建或修改快捷方式   需要从桌面使用获取特殊目录   Environment.SpecialFolder.DesktopDirectory

     

可以在http://www.codeproject.com/Articles/146757/Add-Remove-Startup-Folder-Shortcut-to-Your-App

找到一个逐步显示的非常好的示例

答案 2 :(得分:0)

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