Home > Enterprise >  How can I get Visual Studio cmd parameters for debugging working?
How can I get Visual Studio cmd parameters for debugging working?

Time:10-08

Currently I'm working on a WPF-App. It should be launched by command line with a single parameter. I defined:

public App([Optional] string[] args)
{
    //string[] args = new string[] { "UK356715586" };
    Console.WriteLine("accessed app");
    if (args.Length == 0)
    {                
        Environment.Exit(-1);
    }
    else
    {
        Console.WriteLine("Before PONumber Setting");
        PONumber = args[0].ToString();
    }

    //PONumber = "UK356715586";
}

I set this debug setting for the given parameter:

Debug setting in Visual Studio with a command line argument and working directory set.

By launching in VS I'm getting:

instance of an object."

"args" war "null".

What can i do?

CodePudding user response:

Do not create a constructor with a parameter declared as [Optional]. It is never assigned. If you remove the attribute, you will even get a compilation error.

Instead use the built in Startup event of the Application type. From the documentation:

A typical Windows Presentation Foundation application may perform a variety of initialization tasks when it starts up, including:

  • Processing command-line parameters.

[...] application-scope properties and command-line parameters can only be used programmatically. Programmatic initialization can be performed by handling the Startup event [...]

Assign an event handler in App.xaml and implement it in App.xaml.cs.

<Application x:Class="YourWpfApp.App"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             StartupUri="MainWindow.xaml"
             Startup="App_OnStartup">
   <Application.Resources>
      <!-- ...your resources. -->
   </Application.Resources>
</Application>
public partial class App : Application
{
   private void App_OnStartup(object sender, StartupEventArgs e)
   {
      Console.WriteLine("accessed app");
      if (e.Args.Length == 0)
      {                
          Shutdown(-1);
      }
      else
      {
          Console.WriteLine("Before PONumber Setting");
          PONumber = e.Args[0];
      }
   }

   // ...other code.
}

An alternative is to override the OnStartup method in your App type. From the documentation:

OnStartup raises the Startup event.

A type that derives from Application may override OnStartup. The overridden method must call OnStartup in the base class if the Startup event needs to be raised.

public partial class App : Application
{
   protected override void OnStartup(StartupEventArgs e)
   {
      base.OnStartup(e);

      Console.WriteLine("accessed app");
      if (e.Args.Length == 0)
      {                
          Shutdown(-1);
      }
      else
      {
          Console.WriteLine("Before PONumber Setting");
          PONumber = e.Args[0];
      }
   }

   // ...other code.
}
  • Related