搜索并删除快捷方式

时间:2014-01-31 09:08:33

标签: c# visual-studio

我正在开发的项目必须在Windows启动时运行我找到了代码,但如果用户不想在启动时启动它,我必须告诉用户搜索排序并删除它,或者只是为该选项创建一个按钮,我知道如何删除这样的文件:

if(File.Exists(@"C:\Users\Maged\Desktop\remainder\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe"))
{
    File.Delete(@"C:\Users\Maged\Desktop\remainder\WindowsFormsApplication1\bin\Debug\WindowsFormsApplication1.exe");
}

我需要让MyStartupShortcut.lnk的程序变为searsh,如果存在则删除它。

这是我创建快捷方式的代码:

void CreateStartupShortcut()
{
    string startupFolder = Environment.GetFolderPath(Environment.SpecialFolder.Startup);
    WshShell shell = new WshShell();
    string shortcutAddress = startupFolder + @"\MyStartupShortcut.lnk";
    IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress);
    shortcut.Description = "A startup shortcut. If you delete this shortcut from your computer, LaunchOnStartup.exe will not launch on Windows Startup";
    shortcut.WorkingDirectory = Application.StartupPath; 
    shortcut.TargetPath = Application.ExecutablePath;
    shortcut.Save();
}

我需要删除此文件,无论导演是谁,有什么帮助吗?

1 个答案:

答案 0 :(得分:0)

使用在shortcutAddress方法中创建的CreateStartupShortcut,应使用以下代码删除快捷方式:

if ( File.Exists(shortcutAddress) )
{
    File.Delete(shortcutAddress );
}