Console.writeline是否可以在Windows应用程序中工作? [英] Will Console.writeline work in Windows Application?

查看:130
本文介绍了Console.writeline是否可以在Windows应用程序中工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace csharptesting
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            Console.WriteLine("Hello world");
            Console.ReadLine();            
            //textBox1.Text = "Hello world";
        }
    }
}


无法看到此代码的任何输出.


Can''t see any output for this code.
Is it possible in Windows Application?

推荐答案

这是最简单的方法:

创建一个常规的Windows应用程序,WPF或窗体.转到项目属性,然后将应用程序类型更改为控制台应用程序".您的应用程序将同时成为控制台应用程序和Windows应用程序 .

Windows应用程序和控制台应用程序不是互斥的. 控制台应用程序"实际上意味着显示控制台".这并不意味着不要创建Windows应用程序". "Windows应用程序"仅表示不显示控制台".顺便说一句,如果您在非控制台应用程序中使用控制台方法,它们将(以某种方式)起作用!仅不显示结果.但是,例如,Console.Beep将始终有效.

-SA
Here is the simplest way:

Create a regular Windows application, WPF or Forms. Go to Project properties and change application type to "Console Application". You application will become a console application and windows application at the same time.

Windows application and console application are not mutually exclusive. "Console application" really means "Show console". It does not mean "Don''t create windows application". "Windows application" simply means "don''t show console". By the way, if you use Console methods in non-console application, they will work (in a way)! Only without showing the results. But, for example, Console.Beep will always work.

—SA


请参见 ^ ].这将帮助您实际在表单应用程序中显示控制台.
See this[^]. This will help you actually display the console in a forms application.


这不是不可能,但是为什么要这么做呢?无论如何回答您的问题,我都使用C#创建了一个简单的Windows应用程序,我只是为您复制了以下代码,

It is not impossible but why would you like to do so? Anyway to answer your question, I created a simple Windows App using C#, I just copied the code below for you,

using System.Windows.Forms;

namespace GUI_Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            AllocConsole();
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
        }

        private void button1_Click(object sender, EventArgs e)
        {
            System.Console.WriteLine("Hello World");
        }

        [System.Runtime.InteropServices.DllImport("kernel32.dll")]
        private static extern bool AllocConsole();
    }
}



请忽略命名约定,它只是一个测试应用.

希望对您有所帮助:)



Please ignore the naming convention, it is just a test app.

Hope it helps :)


这篇关于Console.writeline是否可以在Windows应用程序中工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆