我刚开始学习C#和VS,尝试使用Console.WriteLine(...)打印一行文字,但它只会在命令提示符中显示。有没有办法让输出显示在输出窗口中呢?
编辑:这是一个控制台应用程序。
另外,如何访问命令行以运行程序?我只能找到使用F5运行的方法,但如果需要输入参数,则无法使用此方法。
我刚开始学习C#和VS,尝试使用Console.WriteLine(...)打印一行文字,但它只会在命令提示符中显示。有没有办法让输出显示在输出窗口中呢?
编辑:这是一个控制台应用程序。
另外,如何访问命令行以运行程序?我只能找到使用F5运行的方法,但如果需要输入参数,则无法使用此方法。
如果是控制台应用程序,Console.WriteLine
会在控制台中写入内容。如果使用Debug.Print
,则会将内容打印到底部的输出选项卡中。
如果想要添加命令行参数,可以在项目属性中找到。单击Project -> [YourProjectName] Properties... -> Debug -> Start Options -> Command line arguments
。这里的文本将在运行应用程序时传递给应用程序。您也可以在构建后通过从bin\Release
或bin\Debug
文件夹中运行它来运行它,通过cmd
或其他方式。我发现这样测试各种参数比每次设置命令行参数更容易。
我也遇到了这个问题,在使用VS2012的前两天。我的控制台输出在哪里?它一闪而过。像https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b这样的有用示例让我感到困惑。
确实,@martynaspikunas提供的方法是可以将Console.WriteLine()替换为Debug.WriteLine()以在IDE中查看输出。这样做很好,输出会一直保留在那里。
但有时你需要在现有代码的许多地方进行更改才能实现这一点。
我找到了一些替代方法...比如只需要使用一个
Console.ReadKey();
class MyLogger : System.IO.TextWriter
{
private RichTextBox rtb;
public MyLogger(RichTextBox rtb) { this.rtb = rtb; }
public override Encoding Encoding { get { return null; } }
public override void Write(char value)
{
if (value != '\r') rtb.AppendText(new string(value, 1));
}
}
Console.SetOut(new MyLogger(richTextBox1));
using System;
#region Write to Console
/*2 ways to write to console
concatenation
place holder syntax - most preferred
Please note that C# is case sensitive language.
*/
#region
namespace _2__CShrp_Read_and_Write
{
class Program
{
static void Main(string[] args)
{
// Prompt the user for his name
Console.WriteLine("Please enter your name");
// Read the name from console
string UserName = Console.ReadLine();
// Concatenate name with hello word and print
//Console.WriteLine("Hello " + UserName);
//place holder syntax
//what goes in the place holder{0}
//what ever you pass after the comma i.e. UserName
Console.WriteLine("Hello {0}", UserName);
Console.ReadLine();
}
}
}
I hope this helps
这里有一个简单的技巧,可以保留控制台及其输出:
int main ()
{
cout << "Hello World" << endl ;
// Add this line of code before your return statement and the console will stay up
getchar() ;
return 0;
}