在桌面上保存 .lnk 文件时出现 COMException
COMException during saving .lnk file on desktop
我的应用程序抛出异常
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
应包括快捷方式文件名,而不仅仅是包含快捷方式文件的文件夹路径。 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
}
我的应用程序抛出异常
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
应包括快捷方式文件名,而不仅仅是包含快捷方式文件的文件夹路径。 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
}