2016-11-13 59 views
0

我想在桌面上创建的C#代码上的快捷方式创建快捷方式,即在桌面上调用PowerShell命令

  1. 打开PowerShell中,
  2. 进口myModule.dll
  3. 清屏,
  4. 显示我所有的命令为myModule.dll

执行C#之后,快捷方式出现在dektop上,但由于某种原因引号设置在整个shortcut.TargetPath的周围。手动删除这些引号后,一切都很好。

如何防止设置这些引号?

我的代码:

object shDesktop = (object)"Desktop"; 
WshShell shell = new WshShell(); 
string shortcutAddress = (string)shell.SpecialFolders.Item(ref shDesktop) + @"\´MyModule.lnk"; 
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutAddress); 
shortcut.Description = "MyModule"; 
shortcut.TargetPath = @"%Windir%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe -noexit -command &{ import-module \\srv\PS\MyModule.dll;clear; get-command -Module MyModule}"; 
shortcut.Save(); 
+2

'shortcut.Arguments' – PetSerAl

+0

就是这样。 thx PetSerAl。 – piccus

回答

1

由于commented by PetSerAl,使用Arguments属性参数传递到可执行文件:

shortcut.TargetPath = @"%Windir%\\system32\\WindowsPowerShell\\v1.0\\powershell.exe"; 
shortcut.Arguments = @"-noexit -command &{ import-module \\srv\PS\MyModule.dll;clear; get-command -Module MyModule}";