使用C#通过targetpath删除/修改快捷方式#
我们有用户将其桌面上的快捷方式文件重命名为我们的应用程序。如果图标为应用程序更改,删除/修改基于targetpath的快捷方式的最佳方法是什么?换句话说,我很难找到文件名,因为它不断变化。使用C#通过targetpath删除/修改快捷方式#
您应该使用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);
}
要删除一个文件,请使用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
你明白什么op是问? – 2012-07-16 20:38:21
我更新了OP来澄清。 – user766595 2012-07-16 20:39:29
@RobertHarvey&user766595我确实知道他的意思,但这里的catch是在谈论文件不用C#代码修改文件 – HatSoft 2012-07-16 20:42:47
这个句柄的名称会改变,如果应用程序没有启动? – user766595 2012-07-17 13:03:02
是的,但显然你应该启动处理文件的应用程序。 – 2012-07-17 16:40:34