0

I am looking to convert a C# (Windows platform application) into a commandline version.

The scenario is: I have implemented a C# (Windows application) in VS 2010. The output of this application is to generate a txt (log) file (in simple explanation).

Now the case is, there is one other application which need to use my this C# application, by calling my C# application from the command line at the run time.

My question is, how is it possible to convert an already existing C# application into commandline application, so that this C# application can be called from the calling (other) program? There is one input parameter which need to be passed on the commandline to my C# application. And then this C# application will process the data according to input parameter and then generate the output log(txt) file.

Added explanation

I am really impressed by the solutions here. Just a bit more expertise is required from readers. I want one application only to work as both commandline application as well Windows-application (forget to mention it before, sorry!), depending on the number of input parameter pass to the application. From this point of view, I have two options to implement it,

1) Make separate functions for both applications (commandline and windows-forms). Call them according to the input parameter pass. In each function implement the complete functionality of each application without disturbing (or going into the code of other application). Also I will be able to re-use 2 main functions, already built in windows-form application into my commandline application after some editing.

Disadvantage: This will make the code size nearly 50% more than case 2.

2) The second idea is same as describe by one of the expert here, to use the same application/functions for commandline as that of already built windows-form application. The only way to distinguish is to look at the input parameter pass, and decide accordingly whether to show the GUI interface or just use the commandline input (and do processing).

Disadvantage: This case will make the code bit messy and difficult to maintain/implement due to extra adding of check for number of input parameter decisions.

Which strategy should I follow for implementation?

halfer
  • 19,824
  • 17
  • 99
  • 186
Asad
  • 521
  • 2
  • 13
  • 30
  • 2
    Take a look at this Stackoverflow question http://stackoverflow.com/questions/807998/how-do-i-create-a-c-app-that-decides-itself-whether-to-show-as-a-console-or-wind to see if it will work for you. – Mark Hall Sep 23 '11 at 03:53

4 Answers4

2

Sure - just:

  1. Create a new VS2010 command-line project

  2. You'll now have a "main ()" (or, in MS-Land, "_tmain()") function instead of a root class.

  3. Cut and paste the relevant code into "main()" (or into a function called by main (), or into a class created from main() - your choice).

  4. Do a search-and-destroy mission to find anyplace where you're doing GUI input, and substitute command line parameters instead.

  5. Parse your command line.

Voila! Done!

paulsm4
  • 114,292
  • 17
  • 138
  • 190
2

You want to get value from command line is not a good reason to convert winform app to console app. You may use,

string[] args = Environment.GetCommandLineArgs();

However you can change application type by opening project properties (right click on project name) and change the Output type.

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
2

You don't have to convert it. Your application can stay as a Windows application. You simply need to handle command line arguments.

To get the command line arguments from ANYWHERE in the application, just use Environment.GetCommandLineArgs();

Ryan Alford
  • 7,514
  • 6
  • 42
  • 56
1

Just don't show the GUI if you get paramater passed in, as when called from the other program.

Orn Kristjansson
  • 3,435
  • 4
  • 26
  • 40