[英]Blank Command window upon running C# in Visual Studio using CTRL + F5
[英]C# Visual studio Ctrl + F5 opens cmd, instead of program console
最近几天我一直在编写一些控制台应用程序,但是最近当我尝试不进行调试而启动时(Cntl + F5),它将打开C:\\ Windows \\ system32 \\ cmd.exe,而不是程序控制台。 当我从F5开始时,一切正常。 我想我弄乱了选项或某些东西,但是我似乎找不到解决它的方法。 请帮助 ?
编辑:这是一些示例代码-它输出带有整数的螺旋矩阵
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace _14_SpiralMatrix
{
class MatrixD
{
static int[,] matrix;
static int sideSize;
static int counter = 1;
static int row = 0;
static int col = 0;
static bool finished = false;
static void Main(string[] args)
{
sideSize = int.Parse(Console.ReadLine());
matrix = new int[sideSize, sideSize];
while (finished == false)
{
GoDown();
GoRight();
GoUp();
GoLeft();
}
Console.WriteLine();
PrintMatrix();
}
static void PrintMatrix()
{
for (int i = 0; i < sideSize; i++)
{
for (int j = 0; j < sideSize; j++)
{
if (matrix[i, j] < 9)
{
Console.Write(matrix[i, j] + " ");
}
else
{
Console.Write(matrix[i, j] + " ");
}
}
Console.WriteLine();
Console.WriteLine();
}
}
static void GoDown()
{
if (matrix[row, col] != 0)
{
finished = true;
}
//assign numbers until it reaches the end of matrix, or already assigned number
while (row < sideSize && matrix[row, col] == 0)
{
matrix[row, col] = counter;
row++;
counter++;
}
//shift row and col so next method can start from the next empty cell
row--;
col++;
}
static void GoRight()
{
//if next empty cell is filled up, than end of spiral is reached
if (matrix[row, col] != 0)
{
finished = true;
}
while (col < sideSize && matrix[row, col] == 0)
{
matrix[row, col] = counter;
col++;
counter++;
}
col--;
row--;
}
static void GoUp()
{
if (matrix[row, col] != 0)
{
finished = true;
}
while (row > -1 && matrix[row, col] == 0)
{
matrix[row, col] = counter;
row--;
counter++;
}
row++;
col--;
}
static void GoLeft()
{
if (matrix[row, col] != 0)
{
finished = true;
}
while (matrix[row, col] == 0)
{
matrix[row, col] = counter;
col--;
counter++;
}
col++;
row++;
}
}
}
根据汤姆(Tom)的建议,我已决定将我的评论作为答案发布,以便使其更加可见(显然也很有帮助)。
如果您正在运行某些防病毒程序(尤其是Avast,但其他程序可能具有相同的效果),则您的防病毒程序可能会抓住控制台窗口,将其关闭,然后通过cmd.exe重新运行您的应用程序。 我认为防病毒软件正在对您的程序进行安全检查。 您可以将可执行文件作为例外记录在防病毒软件中,也可以暂时禁用防病毒软件(出于明显的安全原因,我建议您将前者优先于后者)。
我不知道OP是否能解决最初的问题,但我正在写这篇文章,希望对其他人有所帮助!
转到工具 -> 选项,然后选择环境 -> 键盘 。 然后单击“ 按快捷键”文本框,然后按Ctrl + F5 。 您应该看到以下内容:
如果您已经具有Debug.StartWithoutDebugging
选项,则右键单击您的项目并选择Properties 。 在左侧列表中选择“ 调试 ”,并验证它是否是“ 启动”项目 (还要验证是否设置了适当的配置):
如果所有设置均正确,则仅是代码完成了什么都不打印。 尝试将Main
方法代码的最后几行更改为此:
Console.WriteLine();
Console.WriteLine("PrintMatrx:");
PrintMatrix();
另请注意,您在此处从键盘读取输入:
sideSize = int.Parse(Console.ReadLine());
在输入内容并按Enter键之前,该程序将不会继续。 您可以像这样使应用程序的行为更具可读性:
Console.Write("Enter side size:");
sideSize = int.Parse(Console.ReadLine());
问题:您的程序实际上没有问题。您的程序正在等待从用户读取整数,而不会在控制台上显示任何文本。 因此,当您从非调试模式运行它时,似乎是命令提示符而不是控制台。
解决方案:只需在Console.ReadLine()
语句之前在控制台上打印一些文本
尝试这个:
static void Main(string[] args)
{
Console.WriteLine("Enter some Integer"); // <-- print some text on console before read statement
sideSize = int.Parse(Console.ReadLine());
您是否尝试过检查Visual Studio许可证是否正确应用?
在安装VS之后尝试测试“ Hello world”控制台应用程序时,我曾经遇到此问题。 我什至不能通过Process Explorer删除控制台进程。
希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.