6

I have searched through Google and here found many examples but I can't seem to get my Windows Form application to run and take arguments form the command line. I really want to have the option of scheduling the application without having a console version. But every time I setup the cmd line arguments it errors out with a CLR20r3 error.

static void Main(string[] args)
{
   if(args != null && args.Length > 0)
   {
      /*
      * arg[1] = Backup Location *require
      * arg[2] = Log File - Enable if present *optional
      * arg[3] = Debug Messages - Enabled if present *optional
      * arg[4] = Backup Type - Type to perform *required
      */

   }
   else
   {
     Application.EnableVisualStyles();
     Application.SetCompatibleTextRenderingDefault(false);
     Application.SetUnhandledExceptionMode(UnhandledExceptionMode.Automatic);
     Application.Run(new Form1());
   }
}

Anytime I try to pass an arg it errors ex

myapp.exe "C:\Backup\" => CLR20r3

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
ondrovic
  • 1,105
  • 2
  • 23
  • 40
  • Is this really all of your code? I don't see anywhere where you **could** pass an argument, based on what you have posted. – Brian Nov 13 '13 at 22:05
  • What exception exacly do you receive? You can find it in Event Viewer probably. – Konrad Kokosa Nov 13 '13 at 22:07
  • 2
    @Brian no not all my code just the start of it to make sure I was doing it right – ondrovic Nov 13 '13 at 22:10
  • Have you tried debugging the app? You can tell your IDE to pass command line args. You can also dump the contents of args to a log file before attempting to parse it, to confirm that the app is receiving what you think it should. –  Nov 13 '13 at 22:11
  • Stopped working Problem signature: Problem Event Name: CLR20r3 Problem Signature 01: sqlbackup.exe Problem Signature 02: 1.0.0.0 Problem Signature 03: 5283ec48 Problem Signature 04: SQLBackup Problem Signature 05: 1.0.0.0 Problem Signature 06: 5283ec48 Problem Signature 07: 1f Problem Signature 08: 15 Problem Signature 09: System.NullReferenceException OS Version: 6.1.7601.2.1.0.256.48 Locale ID: 1033 – ondrovic Nov 13 '13 at 22:14
  • T think you may actually be trying to ask this question. How can I create a WinForms app that runs as a forms app normal, but when given appropriate command line arguments does not required a window or access the desktop. Can you confirm this? – Gary Walker Nov 13 '13 at 22:16
  • 5
    `System.NullReferenceException` because args in Main do not work for Windows Forms application, you should use `string[] args = Environment.GetCommandLineArgs()` – Konrad Kokosa Nov 13 '13 at 22:30
  • @GaryWalker yes this is exactly what I am trying to do pretty much I want to pass the 4 arguments and have the app run IE as a scheduled task – ondrovic Nov 13 '13 at 23:33

1 Answers1

8

This is an example of the startup code I use in a project that runs as a form app or as a formless app depending on command line arguments.

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

namespace BuildFile
{
  static class Program
  {
    /// <summary>
    /// The main entry point for the application.
    /// </summary>
    [STAThread]
    static void Main()
    {
      int ABCFile = -1;
      string[] args = Environment.GetCommandLineArgs();
      if ((args.Length > 1) && (args[1].StartsWith("/n")))
      {
            ... unrelated details omiited
            ABCFile = 1;
        }
      }

      if (ABCFile > 0)
      {
        var me = new MainForm(); // instantiate the form
        me.NoGui(ABCFile); // call the alternate entry point
        Environment.Exit(0);
      }
      else
      {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new MainForm());
      }
    }
  }
}

Note this only works because nothing in my alternative entry point depends on events, etc. that are provided by the runtime environment Application.Run() method which includes handling windows messages, etc.

Gary Walker
  • 8,831
  • 3
  • 19
  • 41