Introduction
Command line argument is a mechanism where a user can pass a set of parameters or the values to a WPF Application when it is executed. These arguments are very important to control an Application from outside, for example, if you want to open a Word document from the command prompt, then you can use this command “C:\> start winword word1.docx” and it will open word1.docx document.
Command line arguments are handled in Startup function. Following is a simple example, which shows how to pass command line arguments to a WPF Application. Let’s create a new WPF Application with the name WPFCommandLine.
- Drag one textbox from the toolbox to the design Window.
- In this example, we will pass a TXT file path to our Application as command line parameter.
- The program will read the TXT file and subsequently write all the text on the text box.
- XAML code given below creates a textbox and initializes it with some properties.
- <Window x:Class="WPFCommandLine.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WPFCommandLine" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525">
- <Grid>
- <TextBox x:Name="textBox" HorizontalAlignment="Left" Height="180" Margin="100" TextWrapping="Wrap" VerticalAlignment="Top" Width="300" /> </Grid>
- </Window>
- Now, subscribe Startup event in an App.xaml file, as shown below.
- <Application x:Class="WPFCommandLine.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WPFCommandLine" StartupUri="MainWindow.xaml" Startup="app_Startup">
- <Application.Resources> </Application.Resources>
- </Application>
- Given below is the implementation of the app_Startup event in an App.xaml.cs, which will get the command line arguments.
- using System.Windows;
- namespace WPFCommandLine {
-
-
-
- public partial class App: Application {
- public static string[] Args;
- void app_Startup(object sender, StartupEventArgs e) {
-
- if (e.Args.Length > 0) {
- Args = e.Args;
- }
- }
- }
- }
- Now, in the MainWindow class, the program will open the TXT file and write all the text on the textbox.
- If some error is found there, then the program will display an error message on the textbox.
- using System;
- using System.IO;
- using System.Windows;
- namespace WPFCommandLine {
- public partial class MainWindow: Window {
- public MainWindow() {
- InitializeComponent();
- String[] args = App.Args;
- try {
- using(StreamReader sr = new StreamReader(args[0])) {
-
-
- String line = sr.ReadToEnd();
- textBox.AppendText(line.ToString());
- textBox.AppendText("\n");
- }
- } catch (Exception e) {
- textBox.AppendText("The file could not be read:");
- textBox.AppendText("\n");
- textBox.AppendText(e.Message);
- }
- }
- }
- }
- When the code given above is compiled and executed, it will produce a blank Window with a textbox because this program needs a command line argument. Thus, Visual Studio provides an easy way to execute your Application with command line parameters.
- Right click on your WPF project in Solution Explorer and select properties. It will display the Window given below.
- Select Debug option and write the file path in the command line argument.
- Create a TXT file with Test.txt and write some text in the file and save it on any location. In this case, the TXT file is saved on “D:\” hard drive.
- Save the changes in your project, compile and execute your Application now. You will see the text in TextBox, which the program reads from the Text.txt file.
Now, let’s try and change the file name on your machine from Test.txt to Test1.txt and execute your program again and you will see that error message in the textbox.
We recommend that you execute the code given above and follow all the steps to execute your Application successfully.