tags:

views:

66

answers:

3

Hey,

As the title say. I know how to do this in C# only, but when trying to do this with WPF I can't find out where or what to add to read the filename when the program starts.

public static void Main(string[] args)
{
    if (Path.GetExtension(args[0]) == ".avi" || Path.GetExtension(args[0]) == ".mkv")
    {
        string pathOfFile = Path.GetFileNameWithoutExtension(args[0]);
        string fullPathOfFile = Path.GetFullPath(args[0]);
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Form1(pathOfFile, fullPathOfFile));
    }
    else
    {
        MessageBox.Show("This is not a supported file, please try again..");
    }
}
+1  A: 

You need to find some entry point (like OnLoad event for your main window) and then access the command line arguments like so:

string[] args = Environment.GetCommandLineArgs();
Assaf Lavie
+1  A: 

Double-click the App.xaml file in Solution Explorer. You can add the Startup event. Its e.Args property gives you access to the command line arguments.

Hans Passant
+1  A: 

Found the solution. Thanks for your help :)

I'll post what I did if anyone else needs it ..

In App.xaml i added Startup="Application_Startup

<Application
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    x:Class="WpfApplication1.App"
    StartupUri="MainWindow.xaml"
    Startup="Application_Startup">
    <Application.Resources>
    <!-- Resources scoped at the Application level should be defined here. -->
    </Application.Resources>
</Application>

And in App.xaml.cs i added these lines:

    public static String[] mArgs;

    private void Application_Startup(object sender, StartupEventArgs e)
    {
        if (e.Args.Length > 0)
        {
            mArgs = e.Args;
        }
    }

Last i had to add some information in the MainWindow class.

public MainWindow()
{
    this.InitializeComponent();
    String[] args = App.mArgs;
}

To get the data you want, you use System.IO.Path

Watch out for the Using statement. if you only use Ex. Path.GetFileNameWithoutExtension you will get a reference error. Use System.IO.Path when getting your data.

Creator84
btw, `(e.Args.Length > 0)` has no sense
abatishchev