C# 在桌面上保存.lnk文件时发生异常

C# 在桌面上保存.lnk文件时发生异常,c#,.net,com-interop,C#,.net,Com Interop,我的应用程序抛出 System.Runtime.InteropServices.COMException 在桌面上保存.lnk文件时。以下是保存快捷方式的代码: private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { var startupFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.De

我的应用程序抛出

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 line throws exception
        }
  • 我的桌面不是只读的

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

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

更新:

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
        }