Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9175299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:53:24+00:00 2026-06-17T16:53:24+00:00

I have written the application which works in console as well as a user

  • 0

I have written the application which works in console as well as a user interface. Now when executing the application from a console, I want to show the message in the currently opened console.

static class Program
    {
        [DllImport("kernel32.dll",
            EntryPoint = "GetStdHandle",
            SetLastError = true,
            CharSet = CharSet.Auto,
            CallingConvention = CallingConvention.StdCall)]
        private static extern IntPtr GetStdHandle(int nStdHandle);
        [DllImport("kernel32.dll",
            EntryPoint = "AllocConsole",
            SetLastError = true,
            CharSet = CharSet.Auto,
            CallingConvention = CallingConvention.StdCall)]
        private static extern int AllocConsole();
        private const int STD_OUTPUT_HANDLE = -11;
        private const int MY_CODE_PAGE = 437; 

        [STAThread]
        static void Main(string[] Args)
        {
                if (Args[0] != "")
                {
                    //AllocConsole();
                    commandlineTool(Args[0]);
                }
                else
                {

                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Form2());
                }
static void commandlineTool(string filename)
{
    //all coding here
    AllocConsole();

    IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
    SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
    FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
    System.Text.Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
    StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
    standardOutput.AutoFlush = true;
    Console.SetOut(standardOutput);

    Console.WriteLine("File has been generated.");
    Console.ReadLine();
}

Actually AllocConsole() allocate new console for the process.

But from this a new console opens and shows the message but I want to show the message in the same console.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:53:25+00:00Added an answer on June 17, 2026 at 4:53 pm

    Method A (preferred, easy):
    You can make the application a console application, and release the console if started in GUI mode using FreeConsole. This IMO is the best solution. The only drawback is that the console window will be shown very briefly when starting the application.

    Method B (use when necessary):
    You can try to attach to the parent process’ console using AttachConsole(ATTACH_PARENT_PROCESS). If the parent process doesn’t have a console this will fail, in which case you’d have to go back to using AllocConsole.

    Also if the System.Console class has already been initialized when you try to attach/allocate the console, you’ll have to re-wire the in/out/error streams like this:

        StreamWriter stdOut = new StreamWriter(Console.OpenStandardOutput());
        stdOut.AutoFlush = true;
        Console.SetOut(stdOut);
    
        StreamWriter stdErr = new StreamWriter(Console.OpenStandardError());
        stdErr.AutoFlush = true;
        Console.SetError(stdErr);
    
        StreamReader stdIn = new StreamReader(Console.OpenStandardInput());
        Console.SetIn(stdIn);
    

    Otherwise you’ll have no output.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a application which runs in the background. I want to write
I have written an application in which I want to convert a .jpg image
I have written very very simple console application which supports some command line options.
I have a console application written in c++. It simply reads an integer from
I have written a script in robotium which works just fine, but our application
I have written a chat application which is working as desired apart from 1
I have written a C++ program (which executes from the command line), that works
I have written an application which collects windows logs from linux, via the Zenoss
I have written an application which simply executes java -jar . I want the
I have written an application which triggers an IP Camera to stream it's data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.