Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 9 of 9

Thread: Open Console and Write to it from Form

  1. #1

    Open Console and Write to it from Form

    Hey,
    I'm wondering how I can have a windows form and then on a button click instantiate a class which opens a console and writes to it aswell as reading input and performing functions (like a normal console app).
    I don't want to have it run a different .exe I want it to be part of the windows form application.

    Can I do this? If so.. how?
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  2. #2
    Heres how to execute a command... but I need to know what language your using. your suppose to put a [C#] [VB.net] ect...

    This is the kind of thing you can just google for... usually. Heres how to execute the command... but I'm sure there is a simple wrapper for the cmd if you just search for it.

    Code:
    public static int ExecuteCommand(string Command, int Timeout)
    {
       int ExitCode;
       ProcessStartInfo ProcessInfo;
       Process Process;
    
       ProcessInfo = new ProcessStartInfo("cmd.exe", "/C " + Command);
       ProcessInfo.CreateNoWindow = true;
       ProcessInfo.UseShellExecute = false;
       Process = Process.Start(ProcessInfo);
       Process.WaitForExit(Timeout);
       ExitCode = Process.ExitCode;
       Process.Close();
    
       return ExitCode;
    }
    Last edited by Templarian; February 6th, 2008 at 09:43 PM.

    Join #kirupa | Click the under my Username | CL | Jeff

  3. #3
    Sorry I thought I mentioned, it's c#.
    Using this can I keep adding commands to the process?
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  4. #4
    Just out of curiosity, why are you calling a Console from a UI-based Windows app? If you want text input/output, there are better (and easier!) ways of doing that.


    Great, now even Kirupa is { facebooking | twittering }

  5. #5
    Can you run console commands from an UI app?
    Like if I wanted to ping something?
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

  6. #6
    http://www.aspdeveloper.net/tiki-ind...PFAQPingServer

    Google is your friend man, learn it use it.

    of course console.write you will just place these variables into your textbox or where every you need them.
    Last edited by Templarian; February 7th, 2008 at 01:52 PM.

    Join #kirupa | Click the under my Username | CL | Jeff

  7. #7

    And the correct answer is ...

    Quote Originally Posted by Templarian View Post
    http://www.aspdeveloper.net/tiki-ind...PFAQPingServer

    Google is your friend man, learn it use it.

    of course console.write you will just place these variables into your textbox or where every you need them.
    Google is not the best answer at all !

    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace MyProject
    {
        public partial class Form1 : Form
        {
            [DllImport("kernel32.dll")]
            public static extern Boolean AllocConsole();
            [DllImport("kernel32.dll")]
            public static extern Boolean FreeConsole();
    
            public Form1()
            {
                AllocConsole();
                InitializeComponent();
            }
    
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                FreeConsole();
            }
        }
    }

  8. #8
    //edit, oops... wrong thread.
    Last edited by Templarian; February 9th, 2008 at 06:22 PM.

    Join #kirupa | Click the under my Username | CL | Jeff

  9. #9
    Great stuff but how do I control it?
    Quote Originally Posted by Dosenfutter View Post
    Google is not the best answer at all !

    Code:
    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;
    using System.Runtime.InteropServices;
    
    namespace MyProject
    {
        public partial class Form1 : Form
        {
            [DllImport("kernel32.dll")]
            public static extern Boolean AllocConsole();
            [DllImport("kernel32.dll")]
            public static extern Boolean FreeConsole();
    
            public Form1()
            {
                AllocConsole();
                InitializeComponent();
            }
    
    
            private void Form1_FormClosing(object sender, FormClosingEventArgs e)
            {
                FreeConsole();
            }
        }
    }
    Web Design and Web Development blog
    Designer Notice: Browser List for design compatibility

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012