Thursday, January 20, 2011

ClickOnce Deployment

In order to use ClickOnce deployment, first of all we have to publish our desktop application to a Web site, FTP server, or to a file path. Then we can use a client script in our Web application to download and run the published desktop application.

1. Go to the Properties of the Windows/WPF project. In the Publish tab set the publish location. Here I have published my application to an IIS Web site.

2. Right click on the Windows/WPF project and select Publish. Follow the publish wizard. After successful Publish, if you go to the physical path of the Web site, published content can be seen as follows.

Then we can register a JavaScript in our ASP.NET Web application to download the desktop application as follows.

Method 1 : window.open
 
///
/// Register client script to launch desktop application.
/// Method 1 : window.open
///

private void LaunchApplicationMethod1()
{
string clientApplicationURL = "http://localhost:82/MyCliclOnceApp.application";

System.Text.StringBuilder jScript = new System.Text.StringBuilder();

jScript.Append("");

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LaunchClientApplicationBlock", jScript.ToString());
}


Method 2 : window.location.href

///
/// Register client script to launch desktop application.
/// Method 2 : window.location.href
///

private void LaunchApplicationMethod2()
{
string clientApplicationURL = "http://localhost:82/MyCliclOnceApp.application";

System.Text.StringBuilder jScript = new System.Text.StringBuilder();

jScript.Append("");

Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "LaunchClientApplicationBlock", jScript.ToString());
}


As you can notice I have passed two arguments to the desktop application through launching client script. They can be consumed in the desktop application as follows.

  
private void Application_Startup(object sender, StartupEventArgs e)
{
try
{
int? clientId = null;
int? clientType = null;

if (ApplicationDeployment.IsNetworkDeployed)
{
// Loaded from the web?
if (ApplicationDeployment.CurrentDeployment.ActivationUri != null)
{
string queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
string value1 = HttpUtility.ParseQueryString(queryString)["ClientId"];

queryString = ApplicationDeployment.CurrentDeployment.ActivationUri.Query;
string value2 = HttpUtility.ParseQueryString(queryString)["ClientType"];

try
{
clientId = int.Parse(value1);
clientType = int.Parse(value2);
}
catch
{ }
}
}
}
catch (Exception ex)
{ }
}


ClickOnce Deployment – Add Custom Desktop Icon for the Shortcut

An application deploying by ClickOnce deployment can be configured to available online only or to available through the start menu. By selecting "The application is available offline as well (launchable from Start menu)" option from the Project Properties -> Publish Tab, it can make available in offline mode as well.

In order to create a shortcut on the user’s desktop for this application, we have to select “Create desktop shortcut” from the “Publish Options” dialog as shown in the following figure. (Publish Tab -> Options…)



This will create a shortcut on the user's desktop with a standard icon. Then to add a custom icon, go to the “Application” tab, browse and select the preferred icon.