none
c# 怎么在指定的目录下创建某个的文件的快捷方式

答案

  • 你好,

     你需要使用“Windows Script Host Object Model”这个COM组件来给文件创建快捷方式。要使用这个组件,先将这个COM组件添加到项目组。简单的代码供你参考。

     void createShortCut(string filePath)
            {
                if (!System.IO.File.Exists(filePath))
                    Console.WriteLine("文件不存在");
                else
                {
                    IWshRuntimeLibrary.WshShell wsh = new IWshRuntimeLibrary.WshShell();
                    //创建快捷键
                    IWshRuntimeLibrary.IWshShortcut shortcut = wsh.CreateShortcut(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\\MyShorcut.lnk") as IWshRuntimeLibrary.IWshShortcut;
                    //要创建快捷方式文件的路径
                    shortcut.TargetPath = filePath;
                    shortcut.Save();
                }
            }

    希望对你有帮助。

    Best Regards,

    Bob


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    2017年1月16日 6:54