如何在c#中为其他应用创建应用的快捷方式?

时间:2012-10-05 07:40:07

标签: c#

我正在尝试使用c#创建一个窗口的表单应用程序,它可以将其他应用程序的快捷方式复制到特殊文件夹。我使用此代码复制文件,但不能缩短...

system.io.file.copy("what","where");

我使用它但不起作用

        System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(".\\calc.exe");
        string destination = @"C:\\ProgramData\\Microsoft\\Windows\\Start Menu\\Programs\Startup";
        System.IO.FileInfo[] files = dir.GetFiles("*.exe");

        foreach (var shorcut in files)
        {
            System.IO.File.Move(shorcut.FullName, destination);
        }

最简单的方法是什么?

1 个答案:

答案 0 :(得分:0)

以下代码允许您阅读lnk文件

它没有多大意义,没有一个简单的方法来检查它。我认为最好的方法是按照应该读取的方式读取.lnk文件。您可以使用COM来执行此操作,ShellLinkObject类实现IShellLink接口。开始使用Project + Add Reference,Browse选项卡并导航到c:\ windows \ system32 \ shell32.dll。这会生成一个互操作库。写这样的代码:

public static string GetLnkTarget(string lnkPath) {
    var shl = new Shell32.Shell();         // Move this to class scope
    lnkPath = System.IO.Path.GetFullPath(lnkPath);
    var dir = shl.NameSpace(System.IO.Path.GetDirectoryName(lnkPath));
    var itm = dir.Items().Item(System.IO.Path.GetFileName(lnkPath));
    var lnk = (Shell32.ShellLinkObject)itm.GetLink;
    return lnk.Target.Path;
}

然后,您只需使用以下代码将其保存在您自己的文件夹中

First include a reference to C:\Windows\System32\wshom.ocx

Second, include the following using statement :-

using IWshRuntimeLibrary;

Third, Here is the code :-

// This creates a Folder Shortcut
IWshShell wsh = new WshShellClass();
IWshShortcut shortcut = (IWshShortcut) wsh.CreateShortcut (shortcutpathfilename);
shortcut.TargetPath = targetdir;
shortcut.Save();
shortcutpathfilename is a path & filename of the .lnk file.
targetdir is the directory the link points to.