Abhilash
Abhilash

Reputation: 245

Running winform application via console

I have a winforms application that takes in input from a gui and runs the logic to give the output.

I want to make a console version of the same application, where I specify the file which has the input values (instead of providing through gui) and run the winform application logic and write the output into a file.

My winform application produces a dll that I wish to use so that any changes in my application is reflected in the console application as well.

Please suggest as to how can I achieve this. I do not know how to proceed.

Upvotes: 1

Views: 367

Answers (2)

Reza Aghaei
Reza Aghaei

Reputation: 125187

You don't need to do an extra job.

Option 1: Environment.GetCommandLineArgs()

You can simplyt use Environment.GetCommandLineArgs() to and parse it and use parameters as you need.

For example:

Environment.GetCommandLineArgs().ToList()
    .ForEach(x =>
    {
        MessageBox.Show(x);
    });

For example you can use your program from command line just like this:

  • yourProgram.exe param1 "another param" anotherParam
  • yourProgram.exe "d:\myfile.txt"

Option 2: static void Main(params string[] args)

Also you can change main signature this way static void Main(params string[] args) and use args this way:

args.ToList()
    .ForEach(x =>
    {
        MessageBox.Show(x);
    });

And use it this way:

  • yourProgram.exe param1 "another param" anotherParam
  • yourProgram.exe "d:\myfile.txt"

Note:

  • You can also use command line arguments at debug time. Open project properties, in Debug tab, in Start Option, enter Command line arguments you need. Then simply run the application.
  • When using Environment.GetCommandLineArgs the first argument is your application executable address.
  • When using args, the first argument is the argument that you passed.

Upvotes: 3

Nytmo
Nytmo

Reputation: 71

Simply add another project (console application) to your solution and put the IO operations there. The logic stays in the class library.

Upvotes: 0

Related Questions