none
window form and console application RRS feed

  • Question

  • Hi

    I have window form application which I would like to run both window form and console (as bat file).

    So, If argument is passed I want to run as console also write to console using console.writeline 

    and If argument is not pass then run as window form.


    Tuesday, June 26, 2018 12:38 AM

All replies

  • I assume that you have different classes, one for running a Form and one for running a Console. If not, you'll need to change things around to have two classes.

    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {
            if (args.Length == 0)
            {
                Form1 f1 = new Form1();
                Application.Run(f1);
            }
            else
            {
                // just an example of how to get it going
                MyConsoleClass myConsole = new MyConsoleClass();
                while (true)
                {
                    Console.WriteLine("Starting Console ...");
                    myConsole.DoStuff(); // or whatever you're going to do here
                    Console.ReadLine();
                }
            }
        }
    }
    


    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    Tuesday, June 26, 2018 12:56 AM
    Moderator
  • Hi Al2012,

    Thank you for posting here.

    According to your description, I am not sure what parameter psssed. You could use IF condition to make a judgement. And then, if the parameter is passed, call the Console.WriteLine() method.

    Set the Console Application is Output type. Right click project> Properties> Application> Choose Console application in Output type.

    I use a gif to show.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, June 26, 2018 8:15 AM
    Moderator
  • You say you want to run as a console if an argument is passed but as a form when an argument is not passed. The problem is, how will you know if there is an argument unless the program executes first and it executes first then how does it switch if necessary? The answer to that question is (somewhat) dependent on things that you do not explain here.

    There are other things that are not clear. Why do you need a console? I am not implying you should not do that, I am just asking for more information.



    Sam Hobbs
    SimpleSamples.Info

    Tuesday, June 26, 2018 2:05 PM