Software for fun and profit
Jonathan Wax
Create shortcut to SL5 OOB App
Hi,
I’m loving the power of Silverlight 5 Out of Browser (OOB) applications, now with improved file access and pInvoke. Thinking about deploying an OOB app to my users, I was wondering if I could create an OOB app that would launch itself when the OS starts.
Looking at how a user invokes the “Install SL OOB” operation by using the built in Silverlight Right Click menu, or a UI that the developer can create which calls the Application.Install() method, they are given 2 options: Desktop and/or Programs. Both of these essentially create a shortcut that launches the Silverlight OOB application using something like this:
"C:\Program Files (x86)\Microsoft Silverlight\sllauncher.exe" 2237722325.localhost
So I assumed I should be able to call the System.IO.File.Copy operation in my elevated trust SL5 OOB application and copy the link from the desktop or programs to the startup folder, but I was wrong. The System.IO.File.Copy operation throws a security exception if I try to write to the Startup folder?! I thought it was suppose to be able to do anything, but apparently NOT!.
pInvoke to the rescue!
Remembering that I could use Win32 APIs with the new pInvoke capabilities in SL5 elevated trust, I quickly replace the File.Copy with CopyFile from kernel32.dll and got it to work.
Enjoy the code.
JW.
Add this declaration to your class:
[DllImport("kernel32.dll", CharSet = CharSet.Unicode, CallingConvention = CallingConvention.StdCall, SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool CopyFile( [MarshalAs(UnmanagedType.LPWStr)]string lpExistingFileName, [MarshalAs(UnmanagedType.LPWStr)]string lpNewFileName, [MarshalAs(UnmanagedType.Bool)]bool bFailIfExists);
Add this function:
private void CreateShortcutInStartUP()
{
try
{
Assembly app = Assembly.GetExecutingAssembly();
//string version = app.FullName.Split(',')[1];
//string fullVersion = version.Split('=')[1];
string ApplicationName = string.Format("{0} Application", app.FullName.Split(',')[0]);
if (ApplicationName != "")
{
string linkName = string.Format("{0}.lnk", ApplicationName);
string DesktopShortcutPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
DesktopShortcutPath = Path.Combine(DesktopShortcutPath, linkName);
string StartUpPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs);
StartUpPath = Path.Combine(StartUpPath, "Startup");
//MessageBox.Show(string.Format("StartUpPath = {0}",StartUpPath));
//MessageBox.Show(string.Format("DesktopShortcutPath = {0}", DesktopShortcutPath));
if (!System.IO.Directory.Exists(StartUpPath))
{
MessageBox.Show(string.Format("Cannot find: {0}", StartUpPath));
return;
}
if (!System.IO.File.Exists(DesktopShortcutPath))
{
MessageBox.Show(string.Format("Cannot find: {0}", DesktopShortcutPath));
return;
}
StartUpPath = Path.Combine(StartUpPath, linkName);
//System.IO.File.Copy(ShortcutPath, newShortcutPath, true);
if (CopyFile(DesktopShortcutPath, StartUpPath, false))
{
MessageBox.Show(string.Format("Copied to: {0}", StartUpPath));
}
else
{
MessageBox.Show("Failed to create Startup Shortcut.");
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Add call it from your startup code.


