Windows Forms 2.0 Programming (Microsoft .NET Development Series)

One other feature you need to support in a programmatic way is command line processing. In a normal application, command line parameters are available from the string array passed to Main:

static void Main(string[] args) { foreach( string arg in args ) { MessageBox.Show(arg); } ... }

Similarly, URLs have a well-known syntax for passing arguments:

http://www.sellsbrothers.com/wahoo2/wahoo.application?columns=10&rows=20

The combination of the two makes it seem natural to pass command line arguments to ClickOnce applications using the special URL syntax. And it is.

First, you activate support by checking the "Allow URL parameters to be passed to an application" check box in VS05, which is available from your project's property pages | Publish | Publish Options, as shown in Figure 19.55.

Figure 19.55. Configuring Support for URL Parameter Passing to ClickOnce-Deployed Applications

ClickOnce makes sure that command line arguments are passed to the application for harvesting, although they are not passed to your application's entry point Main method, as per standard command line arguments. Instead, they are available from the application's activation URL in a query string format, which we need to parse appropriately:

// Program.cs using System.Collections.Specialized; using System.Deployment.Application; ... static class Program { [STAThread] static void Main(string[] args) { Application.EnableVisualStyles(); int columns = 10; int rows = 20; // Query string or command line args?? if( ApplicationDeployment.IsNetworkDeployed ) { string activationUri = ApplicationDeployment.CurrentDeployment.ActivationUri.AbsoluteUri; if( !string.IsNullOrEmpty(activationUri) ) { Uri uri = new Uri(activationUri); if( !string.IsNullOrEmpty(uri.Query) ) { // Parse (expecting format: "?columns=Xxx&rows=Xxx") string query = uri.Query.ToLower(); GetQueryArg(query, "columns", ref columns); GetQueryArg(query, "rows", ref rows); } } } else { // Process command line args as usual ... } Application.Run(new MainForm(columns, rows)); } // A query string extraction helper static bool GetQueryArg&lt;T&gt;(string query, string arg, ref T value) { Regex regex = new Regex(arg + "=(?<value>[^&]*)"); Match match = regex.Match(query); if( match == null ) { return false; } string s = match.Groups["value"].Value; if( string.IsNullOrEmpty(s) ) { return false; } TypeConverter converter = TypeDescriptor.GetConverter(typeof(T)); if( !converter.CanConvertFrom(typeof(string)) ) { return false; } value = (T)converter.ConvertFrom(s); return true; } }

Command line arguments for a URL are available only if the application was launched from a server, and this state is reflected by ApplicationDeployment.IsNetworkDeployed.[32] If this property is true, we retrieve and parse the query string using the GetQueryArg helper function I whipped up. If ApplicationDeployment.IsNetworkDeployed returns false, we check for normal command line arguments and process as usual.

[32] Remember, online/offline applications can be launched from the local machine via the Start menu shortcut.

Категории