Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Nice and clean. (<strong>.NET 4.0</strong>)</p> <pre><code>Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object dynamic shell = Activator.CreateInstance(t); try{ var lnk = shell.CreateShortcut("sc.lnk"); try{ lnk.TargetPath = @"C:\something"; lnk.IconLocation = "shell32.dll, 1"; lnk.Save(); }finally{ Marshal.FinalReleaseComObject(lnk); } }finally{ Marshal.FinalReleaseComObject(shell); } </code></pre> <p>That's it, no additional code needed. <em>CreateShortcut</em> can even load shortcut from file, so properties like <em>TargetPath</em> return existing information. <a href="http://msdn.microsoft.com/en-us/library/f5y78918%28v=vs.84%29.aspx">Shortcut object properties</a>.</p> <p>Also possible this way for versions of .NET unsupporting dynamic types. (<strong>.NET 3.5</strong>)</p> <pre><code>Type t = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); //Windows Script Host Shell Object object shell = Activator.CreateInstance(t); try{ object lnk = t.InvokeMember("CreateShortcut", BindingFlags.InvokeMethod, null, shell, new object[]{"sc.lnk"}); try{ t.InvokeMember("TargetPath", BindingFlags.SetProperty, null, lnk, new object[]{@"C:\whatever"}); t.InvokeMember("IconLocation", BindingFlags.SetProperty, null, lnk, new object[]{"shell32.dll, 5"}); t.InvokeMember("Save", BindingFlags.InvokeMethod, null, lnk, null); }finally{ Marshal.FinalReleaseComObject(lnk); } }finally{ Marshal.FinalReleaseComObject(shell); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload