在桌面上保存.lnk文件时出现COMException

时间:2016-05-29 09:25:20

标签: c# .net com-interop

我的应用程序被抛出

  

System.Runtime.InteropServices.COMException

在桌面上保存.lnk文件。错误行由应用程序显示。

 private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
            var startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var shell = new WshShell();
            var windowsApplicationShortcut = (IWshShortcut)shell.CreateShortcut(startupFolderPath);
            windowsApplicationShortcut.Description = "Network Folder";
            windowsApplicationShortcut.WorkingDirectory = @"Z:\";
            windowsApplicationShortcut.TargetPath = @"Z:\";
            windowsApplicationShortcut.IconLocation = Application.StartupPath + @"\img\normal.ico";
            windowsApplicationShortcut.Save(); // this thrown error
        }

我的桌面不是只读的。

1 个答案:

答案 0 :(得分:0)

var windowsApplicationShortcut = (IWshShortcut)shell.CreateShortcut(startupFolderPath);

startupFolderPath应该包含快捷方式文件名,而不仅仅是包含快捷方式文件的文件夹路径。 MSDN ref

<强>更新

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
    {
            var startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
            var inkRef = startupFolderPath  + @"\img\normal.ico";
            var shell = new WshShell();
            var windowsApplicationShortcut = (IWshShortcut)shell.CreateShortcut(inkRef);
            windowsApplicationShortcut.Description = "Network Folder";
            windowsApplicationShortcut.WorkingDirectory = @"Z:\";
            windowsApplicationShortcut.TargetPath = @"Z:\";
            windowsApplicationShortcut.IconLocation = Application.StartupPath + @"\img\normal.ico";
            windowsApplicationShortcut.Save(); // this thrown error
        }