Start Menu Shortcuts in C#
-
Is there an alternative way to create shortcuts in C# other than using the Wshell? Because everytime i include my app and distribute it to others, some people don't have the wscript.exe and even if they DID install it, they get this kind of error : "IWshShell3 failed". Are there any workarounds to this?
-
Is there an alternative way to create shortcuts in C# other than using the Wshell? Because everytime i include my app and distribute it to others, some people don't have the wscript.exe and even if they DID install it, they get this kind of error : "IWshShell3 failed". Are there any workarounds to this?
You can use the COM ShellLinkObject to create shortcuts, and this only requires shell32.dll. In an ideal world, things would be simple and you would just have to add a reference to shell32 in the VS.NET IDE, then start playing with the objects and interfaces. Unfortunately, the actual IShellLink interface is not declared in the shell32 namespace. You'll find ShellLinkObject and ShellLinkObjectClass but they are worthless. That's somewhat tough since you have to do the interop yourself, ie declare COM signatures. Here is how it goes :
[ComImport, Guid("00021401-0000-0000-C000-000000000046")]
class ShellLink
{
}[Guid("000214ee-0000-0000-c000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
interface IShellLinkA // Cannot list any base interfaces here
{
// Note that IUnknown Interface members are NOT listed here:void SetPath(string path);
void SetDescription(string path);
void Save(string where);...
}// and that's used with code like this :
ShellLink c = new ShellLink();
IShellLinkA i = (IShellLinkA) c;
i.SetPath("ert");
...That's just the begin of the code. Be sure to read this reference MS article[^] (WIN32 C/C++ audience though).
-
You can use the COM ShellLinkObject to create shortcuts, and this only requires shell32.dll. In an ideal world, things would be simple and you would just have to add a reference to shell32 in the VS.NET IDE, then start playing with the objects and interfaces. Unfortunately, the actual IShellLink interface is not declared in the shell32 namespace. You'll find ShellLinkObject and ShellLinkObjectClass but they are worthless. That's somewhat tough since you have to do the interop yourself, ie declare COM signatures. Here is how it goes :
[ComImport, Guid("00021401-0000-0000-C000-000000000046")]
class ShellLink
{
}[Guid("000214ee-0000-0000-c000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsDual)]
interface IShellLinkA // Cannot list any base interfaces here
{
// Note that IUnknown Interface members are NOT listed here:void SetPath(string path);
void SetDescription(string path);
void Save(string where);...
}// and that's used with code like this :
ShellLink c = new ShellLink();
IShellLinkA i = (IShellLinkA) c;
i.SetPath("ert");
...That's just the begin of the code. Be sure to read this reference MS article[^] (WIN32 C/C++ audience though).
i put in this : [ComImport, Guid("00021401-0000-0000-C000-000000000046")] class ShellLink { } [Guid("000214ee-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsDual)] interface IShellLinkA // Cannot list any base interfaces here { void SetPath(string path); void SetDescription(string path); void SetIconLocation(string path, int index); void Save(string where); void SetWorkingDirectory(string path); } and called this: ShellLink c = new ShellLink(); IShellLinkA i = (IShellLinkA) c; i.SetPath(@"c:\test.txt"); i.Save(@"c:\test.lnk"); but it doesn't work (the shortcut doesn't appear). And i don't quite understand some of the stuff/code on the link you gave me (mainly because it's in C/C++). Any help would be greatly appreciated.
-
i put in this : [ComImport, Guid("00021401-0000-0000-C000-000000000046")] class ShellLink { } [Guid("000214ee-0000-0000-c000-000000000046"), InterfaceType(ComInterfaceType.InterfaceIsDual)] interface IShellLinkA // Cannot list any base interfaces here { void SetPath(string path); void SetDescription(string path); void SetIconLocation(string path, int index); void Save(string where); void SetWorkingDirectory(string path); } and called this: ShellLink c = new ShellLink(); IShellLinkA i = (IShellLinkA) c; i.SetPath(@"c:\test.txt"); i.Save(@"c:\test.lnk"); but it doesn't work (the shortcut doesn't appear). And i don't quite understand some of the stuff/code on the link you gave me (mainly because it's in C/C++). Any help would be greatly appreciated.
In my code snippet, IShellLink was declared as a dual interface, although, after I have double checked it, it's only a IUnknown interface. Here is the right declaration and code :
[ComImport, Guid("00021401-0000-0000-C000-000000000046")]
class SH
{
}[Guid("000214ee-0000-0000-c000-000000000046"),
InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
interface IShellLinkA // Cannot list any base interfaces here
{
// Note that IUnknown Interface members are NOT listed here:
void GetPath(string pszFile,
int cchMaxPath,
IntPtr pfd,
int fFlags);void GetIDList(IntPtr ppidl);
void SetIDList(IntPtr pidl);
void GetDescription(string pszName,
int cchMaxName);void SetDescription(string pszName);
void GetWorkingDirectory(string pszDir,
int cchMaxPath);void SetWorkingDirectory(string pszDir);
void GetArguments(string pszArgs, int cchMaxPath);
void SetArguments(string pszArgs);
IntPtr GetHotkey();
void SetHotkey(int wHotkey);
void Resolve(IntPtr hwnd,
int fFlags);void SetPath(string pszFile);
}//
SH c = new SH();
IShellLinkA i = (IShellLinkA) c;
...
UCOMIPersistFile p = (UCOMIPersistFile) i;
p.Save(@"c:\mylink.lnk",false);