RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Take the Pain Out of Deployments with ClickOnce

Deploying Windows applications in the age of client-server computing has never been easy. But smart client architecture offers a revolution that makes deployments and maintenance stress-free. Find out how to use the ClickOnce technology to turn your clients into smart clients.

ne of the most challenging tasks faced by Windows application developers is the deployment of their applications on the client machines. Once an application is deployed, any changes or maintenance to the application require redeployment. Worse, with so many different client configurations, updating a Windows application is always fraught with unknowns.

In Visual Studio 2005, a new deployment technology known as ClickOnce makes such deployments and even updates extremely easy and painless. ClickOnce is the perfect technology to ease the deployment of smart clients.

A smart client, of course, is basically a Windows application that leverages the system's local resources and is able to intelligently connect to distributed data sources (such as Web services) as needed.

Ubiquitous access was the key advantage that helped propel the popularity of Web-based applications in the enterprise. And while today a lot of companies are still using Web-based apps, network latencies and server delays are some of the issues that companies struggle with. Common frustrations include slow response time from Web sites and limited functionality (due to the stateless nature of the HTTP protocol). A smart client aims to reap the benefit of the rich functionality of the client (Windows) while utilizing the power of Web services to provide ubiquitous data access on the back end. ClickOnce provides a critical piece of enabling infrastructure for a smart client: It allows you to deploy applications onto users' computers with the same ease that Web-based applications are accessed.

To illustrate how ClickOnce works, I will build a simple Windows application and then publish and deploy the application using a Web server. Furthermore, because ClickOnce has the added benefit of automatically checking for the latest update of the application (you can configure the application to check every time before it runs or to check at regular time intervals), I will also show you how to configure the application with the minimum set of permissions needed for it to perform these checks properly and safely.

Creating the UI
The sample application is a simple text editor. Using this text editor, you can open a file for editing, save a file to disk, as well as perform a simple spell-check using a spell-check Web service.

To build the sample app, launch Visual Studio 2005 and create a new Windows application. Name it C:\MyEditor. Populate the default Form1 with the controls as shown in Figure 1.

Figure 1. Buiding the UI: Populate the default Form1 with the various controls shown.
Figure 2. Add a Menu: Add and populate a ContextMenuStrip control on Form1.

Next, add a ContextMenuStrip control from the Toolbox and add a menu item into the control, as shown in Figure 2.

Set the ContextMenuStrip property of RichTextBox1 to "ContextMenuStrip1" so that when the user right-clicks on the RichTextBox control, a context menu is displayed.

Adding a Web Service
In this section I will add a Web service to the project. The text editor will connect to this Web service for the spell-check functionality. Go to File—>Add—>New Web Site…. Select the ASP.NET Web Service template and name the project http://localhost/SpellCheckWS (see Figure 3).

Figure 3. Spell Check: Create a new Web service project to provide the spell-check functionality for the smart client text editor.
In the Service1.vb file, code the following:

    <WebMethod()> _
    Public Function CheckWord(ByVal word As String) As String()
        Dim result(0) As String
        result(0) = ""
        Select Case word
            Case "teh" : ReDim result(0)
                result(0) = "the"
            Case "comuter" : ReDim result(2)
                result(0) = "commuter"
                result(1) = "computer"
                result(2) = "commuters"
        End Select
        Return result
    End Function
Essentially, the CheckWord() function takes a string parameter and returns an array of strings. For simplicity I have hard-coded the words to spell-check.

Consuming the Web Service
To consume the Web service, right-click on the MyEditor project in Solution Explorer and select Add Web Reference…. In the Add Web Reference dialog, enter the URL as shown in Figure 4 and then click Go. Finally, click Add Reference and then OK.

Figure 4. Web Service URL: Add a Web reference to the Web service.
Double-click on the Spell Check item in the ContextMenuStrip control and code the following:

    Private Sub SpellCheckToolStripMenuItem_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles SpellCheckToolStripMenuItem.Click

        '---use the web service to spell-check a word
        Dim ws As New SpellCheckWS.Service
        Dim result() As String
        result = ws.CheckWord(RichTextBox1.SelectedText)
        Dim str As String = ""
        For i As Integer = 0 To result.Length - 1
            str += result(i) & ", "
        '---display the list of possible words
    End Sub
The above block of code will connect to the spell-check Web service and display the result returned by it.

Likewise for the Open button, code the following:

Private Sub btnOpen_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles btnOpen.Click
        ' Create an OpenFileDialog to request a file to open.
        Dim openFile1 As New OpenFileDialog()

        ' Initialize the OpenFileDialog to look for RTF files.
        openFile1.DefaultExt = "*.rtf"
        openFile1.Filter = "RTF Files|*.rtf"

        ' Determine whether the user selected a file from the 
        ' OpenFileDialog.
        If (openFile1.ShowDialog() = _
            System.Windows.Forms.DialogResult.OK) _
            And (openFile1.FileName.Length > 0) Then

            ' Load the contents of the file into the RichTextBox.
            RichTextBox1.LoadFile(openFile1.FileName, _
        End If
    End Sub
The above block of code will display the Open File dialog to let the user choose a filename. The chosen file is then loaded onto the RichTextBox control.

For the Save button, code the following. This will open the Save File dialog and save the content of the RichTextBox control onto the selected file.

Private Sub btnSave_Click( _
   ByVal sender As System.Object, _
   ByVal e As System.EventArgs) _
   Handles btnSave.Click
        ' Create a SaveFileDialog to request a path and file name
        ' to save to.
        Dim saveFile1 As New SaveFileDialog()

        ' Initialize the SaveFileDialog to specify the RTF extension 
        ' for the file.
        saveFile1.DefaultExt = "*.rtf"
        saveFile1.Filter = "RTF Files|*.rtf"

        ' Determine if the user selected a file name from the 
        ' saveFileDialog.
        If (saveFile1.ShowDialog() = _
            System.Windows.Forms.DialogResult.OK) _
            And (saveFile1.FileName.Length) > 0 Then

            ' Save the contents of the RichTextBox into the file.
            RichTextBox1.SaveFile(saveFile1.FileName, _
        End If
    End Sub
That's it! To test the application, press F5. Type something into the RichTextBox control, right-click on a word and then select Spell Check. The result returned by the Web service will be displayed in dialog that offers suggestions for the misspelled word.

When you click the Save… button, a Save As dialog box will appear. Likewise, when you click the Open… button, the Open dialog box will appear. Try loading a file from disk and make some changes to it. Then, save the file. You should now have a functional simple text editor.

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date