TOC

This article is currently in the process of being translated into Turkish (~23% done).

Bir WPF Uygulaması:

Command-line parameters in WPF

"Komut Satırı Parametreleri" bir uygulamayı başlatırken verdiğimiz parametreler sayesinde o uygulamanın davranışlarını değiştirmemizi sağlayan bir tekniktir. En sık görülen örnek, bir uygulamayı istediğimiz bir dosyayla açmaktır (ör: Metin düzenleyicisi). Bunu Windows'un içinde gelen "Not Defteri" uygulamasıyla siz de deneyebilirsiniz. Başlat > Çalıştır butonu veya [Windows Tuşu + R] tuş kombinasyonuylar Çalıştır'ı başlatın.

notepad.exe C:\Windows\win.ini

Bu eylem "Not Defteri" uygulamasını win.ini dosyasıyla birlikte açacaktır (yolu kendi sisteminize göre değiştirmeniz gerekebilir). Not Defteri bir ya da daha fazla parametreyi kontrol ediyor ve daha sonra bunları kullanıyor. Bunu sizin uygulamanız da yapabilir!

Command-line parameters are passed to your WPF application through the Startup event, which we subscribed to in the App.xaml article. We will do the same in this example, and then use the value passed on to through the method arguments. First, the App.xaml file:

<Application x:Class="WpfTutorialSamples.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
			 Startup="Application_Startup">
    <Application.Resources></Application.Resources>
</Application>

All we do here is to subscribe to the Startup event, replacing the StartupUri property. The event is then implemented in App.xaml.cs:

using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfTutorialSamples
{
	public partial class App : Application
	{

		private void Application_Startup(object sender, StartupEventArgs e)
		{
			MainWindow wnd = new MainWindow();
			if(e.Args.Length == 1)
				MessageBox.Show("Now opening file: \n\n" + e.Args[0]);
			wnd.Show();
		}
	}
}

The StartupEventArgs is what we use here. It's passed into the Application Startup event, with the name e. It has the property Args, which is an array of strings. Command-line parameters are separated by spaces, unless the space is inside a quoted string.

Testing the command-line parameter

If you run the above example, nothing will happen, because no command-line parameters have been specified. Fortunately, Visual Studio makes it easy to test this in your application. From the Project menu select "[Project name] properties" and then go to the Debug tab, where you can define a command-line parameter. It should look something like this:

Try running the application and you will see it respond to your parameter.

Of course, the message isn't terribly useful. Instead you might want to either pass it to the constructor of your main window or call a public open method on it, like this:

using System;
using System.Collections.Generic;
using System.Windows;

namespace WpfTutorialSamples
{
	public partial class App : Application
	{

		private void Application_Startup(object sender, StartupEventArgs e)
		{
			MainWindow wnd = new MainWindow();
			// The OpenFile() method is just an example of what you could do with the
			// parameter. The method should be declared on your MainWindow class, where
			// you could use a range of methods to process the passed file path
			if(e.Args.Length == 1)
				wnd.OpenFile(e.Args[0]);
			wnd.Show();
		}
	}
}

Command-line possibilities

In this example, we test if there is exactly one argument and if so, we use it as a filename. In a real world example, you might collect several arguments and even use them for options, e.g. toggling a certain feature on or off. You would do that by looping through the entire list of arguments passed while collecting the information you need to proceed, but that's beyond the scope of this article.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!