How to: Create a C# Console Application

The purpose of this topic is to acquaint you with the Visual C# Express Edition development environment as you build the simplest form of C# program, a console application. Because console applications perform all their input and output at the command line, they are ideal for quickly trying out language features and writing command-line utilities.


The features of the development environment discussed in this section will also be encountered when you develop Windows Forms applications. Don't skip this part just because you don't plan on writing console applications!

In this section, you'll learn:

  • How to create a new console application.

  • How to use bookmarks in the Code editor.

  • How to view Solution Explorer.

  • How to keep your code nicely formatted.

  • How to use IntelliSense to make entering code faster and more accurate.

  • How to build and run your application.

The program that you create in this task uses the classes in the System.IO namespace to obtain and display a list of files and file sizes in the C:\ directory. You could use this code as a basis for a utility that searches a directory for a particular file name.

link to video For a video version of this topic, see Video How to: Create a C# Console Application.

To create a C# console application

  1. On the File menu, click New Project.

    The New Project dialog box appears. This dialog box lists the different default application types that Visual C# Express Edition can create.

  2. Select Console Application as your project type and change the name of your application to List Files.

    The default location should be fine, but you can always enter a new path if you want.

  3. Click OK.

    Visual C# Express Edition creates a new folder for your project named after the project title. It also opens the main Visual C# Express Edition window and the Code pane where you will enter and modify the C# source code that creates your application.

    ExpressCS screenshot

    Notice the toolbar at the top of the window. This toolbar contains icons for creating, loading and saving projects, editing source code, building your application, and hiding and displaying other windows that make up the Visual C# Express Edition environment. The five icons at the far right of this toolbar are used to open important windows such as Solution Explorer and the Toolbox. Place the mouse pointer over any one of these icons to obtain pop-up tooltip Help.


    Bookmarks are useful when you write large programs, as they enable you to quickly jump from one location in the source code to another. To create a bookmark, click the Toggle bookmark icon, or press CTRL+B, T. You see a cyan marker in the margin. Use the same procedure to delete an existing bookmark. You can create as many bookmarks as you like, and jump between them using the Next and Previous Bookmark icons, or by pressing CTRL+B, N, and CTRL+B, P.

  4. Make sure that Solution Explorer is visible by clicking the Solution Explorer tab on the right of the screen, or the Solution Explorer icon in the toolbar.

    Solution Explorer is a very useful pane as it displays the various files that make up your project. The most important file in this project is the file "Program.cs," which contains the source code for your application.


    Knowing how to open and hide windows like Solution Explorer is important if you want to keep your Visual C# Express Edition display nice and tidy. By default, Solution Explorer is visible. If you want to hide Solution Explorer, click the Auto Hide icon, the push-pin icon in its title bar, or open the Options menu on the title bar of Solution Explorer and enable Auto Hide. Other windows, such as Class View and Properties, also have these icons.

  5. If Solution Explorer is still obscuring the Code pane, click in the Code pane to hide it.

    Now click to the right of the open curly brace ({) inside the Main method, and press ENTER to start a new line. Notice how the editor automatically indents the cursor.


    The Code Editor always tries to keep your code formatted in a standard, easy-to-read layout. If your code ever starts to look messy, you can reformat all of the document by clicking Advanced, and then Format Document from the Edit menu, or by pressing CTRL+E, D.

  6. Type the class name Console into the Code Editor.

    When you type a C# class name or keyword, you have a choice: you can either type the complete word yourself, or let the IntelliSense tool that is part of the Code pane do it for you. For example, when you type a "c", a pop-up list of words appears as IntelliSense tries to predict the word you are typing. In this case, you won't see the word "Console" displayed just yet. Either scroll down the list, or continue typing the word "console." When "console" appears highlighted in the list, press ENTER, or TAB, or double-click it, and Console will be added to your code.

    ExpressCase screenshot

    The advantage of using IntelliSense is that you can be certain the casing and the spelling are correct. Whether you type the code or let IntelliSense do it is completely up to you.

  7. Type a period and the method name WriteLine.

    As soon as you type the period after Console, another IntelliSense list is displayed. This list contains all the possible methods and properties that are part of the Console class. You want the WriteLine method, and you can see it at the bottom of the list. Either finish typing WriteLine yourself, or press the DOWN ARROW key until it is selected, and press ENTER, or TAB, or double-click it. WriteLine will be added to your code.

    Type an opening parenthesis. You'll immediately see another IntelliSense feature, the method signatures, appear as a tooltip message. In this case, you can see that there are 19 different signatures, and you can look through them by clicking the UP and DOWN ARROW keys. The following illustration shows this tooltip.


  8. Type the string "This program lists all the files in the directory."

    Type the message enclosed in quotation marks and add a closing parenthesis. You'll see a red wavy underline displayed as a reminder that something is missing. Type a semicolon (;) and the underline will disappear.

  9. Finish the program.

    Type or copy and paste the following code after the line Console.WriteLine("This program lists all the files in the directory.") to complete the program:

    System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(@"C:\");
    foreach (System.IO.FileInfo file in dir.GetFiles("*.*"))
        Console.WriteLine("{0}, {1}", file.Name, file.Length);

    The last line in the program is Console.ReadLine(); which causes the program to pause until ENTER is pressed. If you omit this line, the command-line window will immediately disappear and you won't be able to see the output of your program. If you are creating a command-line utility that will always be used from the command-line console, you will probably want to omit the call to the ReadLine() method.

  10. Run your program.

    Your first program is now complete and ready to compile and run. To do this, either press F5 or click on the Start icon in the toolbar.

    VJS Express List Files Start

  11. Once the program compiles and runs, the Console window opens and a list of files and their sizes is displayed. Press ENTER to exit the program.

    If you are new to C# programming, this would be a good time to read the C# Language Primer section, and try some of the language examples. If you want to know more about the Visual C# Express Edition development environment, and how to create Windows Applications, move on to the next section How to: Create a C# Windows Forms Application.

See Also


How to: Create a C# Console Application

How to: Create a C# WPF Application

How to: Create a New Visual C# Express Application


C# Language Primer

Other Resources

Creating Your First Visual C# Application

Visual C# Express Tips and Tricks

Change History




September 2008

Reorganized coding instructions to make them clearer.

Customer feedback.