简体   繁体   中英

C# Visual studio Ctrl + F5 opens cmd, instead of program console

I've been writing some console applications for the last few days, but recently when I try to start without debugging(Cntl+F5) it opens C:\\Windows\\system32\\cmd.exe, instead of the program console. When I start it with F5, everything works fine. I guess I messed with the options or something, but I can't seem to find a way to fix it. Help please ?

Edit: Here's some example code - it prints a spiral matrix with integers

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++;

        }


    }
}

Per Tom's advice, I've decided to post my comment as an answer so it's more visible (apparently it's also helpful).

If you are running certain anti-virus programs (particularly Avast, but others might have the same effect), then your anti-virus program may be grabbing your console window, closing it, and then re-running your application via cmd.exe. I assume the anti-virus is doing a security check on your program. You can either log your executable as an exception in your anti-virus or temporarily disable your anti-virus (I recommend the former over the latter for obvious security reasons).

I don't know if OP figured out the original problem, but I'm writing this in the hopes it will help others!

Go to Tools -> Options and choose Environment -> Keyboard . Then click on Press shortcut keys text box and press Ctrl+F5 . You should see this:

在此处输入图片说明

If you have the option Debug.StartWithoutDebugging already, then right click your project and choose Properties . Choose Debug in the list to the left, and verify that it's Start project (also verify that the appropriate configuration is set):

在此处输入图片说明

If all is set correctly, then it's just that your code finishes printing nothing. Try changing last few lines of your Main method code to this:

Console.WriteLine();
Console.WriteLine("PrintMatrx:");
PrintMatrix();

Also note, that you read input from the keyboard here:

sideSize = int.Parse(Console.ReadLine());

The program won't continue until you'll enter something and press the enter key. You can make the behaviour of your application more readable like this:

Console.Write("Enter side size:");
sideSize = int.Parse(Console.ReadLine());

Problem : actually there is no problem with your program.your program is waiting to read the integer from user without displaying any text on console. so that when you run it from non-debug mode it seems like it is Command prompt instead of your console.

Solution : just print some text on console before Console.ReadLine() statement

Try This:

   static void Main(string[] args)
    {
        Console.WriteLine("Enter some Integer"); // <-- print some text on console before read statement
        sideSize = int.Parse(Console.ReadLine());

Have you tried to check your Visual Studio license if it was properly applied?

I used to meet this issue when i tried to test the "Hello world" console app just after installing the VS. I even coult not delete the console process by Process Explorer.

Hope this can help.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM