1

I have an WinForms application. I wan't to run app sometimes with parameter, like this:

myapp.exe -d

which tells app to do some unusual actions like write a log, or show some additional info.

What is the best way to do it?

The first came to my mind was an adding main function args reading, and when parameter passed then create some flag and then use it to do some additional actions.

But maybe there is more beautiful and right way for this?

ChrisF
  • 134,786
  • 31
  • 255
  • 325
Ksice
  • 3,277
  • 9
  • 43
  • 67

3 Answers3

1

If this is a feature that is going to be rarely used, consider using:

Trace.WriteLine("");

Then use something like DebugView whenever you need to...

http://technet.microsoft.com/en-us/sysinternals/bb896647.aspx

Robbie Dee
  • 1,939
  • 16
  • 43
1

Actually the best way to this is using args. The sole purpose of having args is this. The way you selected is the best way to it.

Geethanga
  • 1,836
  • 4
  • 20
  • 31
1

Command line arguments are a fine way to do this. You'll need to process them from the arguments to Main, probably setting some global (readonly) state which later processing can check.

(To avoid globals – eg. for unit testing – you could use dependency injection into the parts of the application that depend on the command line to make mocking easier.)

Richard
  • 106,783
  • 21
  • 203
  • 265