Share via


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. Console applications perform all their input and output at the command line so they are ideal for quickly trying out language features and writing command-line utilities.

Note

The features of the development environment discussed in this section will also be encountered when developing Windows Forms applications, so don't skip this part simply 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 you create in this task makes use of the classes in the System.IO namespace to obtain and display a list of all the files, and their sizes, contained in the C:\ directory. You could use this code as a basis for a utility that searches a directory for a particular filename.

To create a C# console application

  1. On the File menu, click New Project.

    The New Project dialog 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, and opens the main Visual C# Express Edition window, including the Code pane where you will enter and edit the C# source code that makes up your application.

    ExpressCS screenshot

    Notice the toolbar at the top of the window. This toolbar contains icons for creating, loading and saving projects, editing your 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 of these icons to get pop-up tool tip Help.

    Note

    Bookmarks are useful when writing large programs, as they allow 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 the Solution Explorer is visible, by clicking on 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.

    ExpressThumbtack

    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, the Solution Explorer is visible. If you would like to hide the Solution Explorer, click the Auto Hide icon, the push-pin icon, in its title bar, or open the Options menu in the Solution Explorer title bar and enable Auto Hide. Other windows, such as Class View and Properties, also have these icons.

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

    If Solution Explorer is still obscuring the Code pane, click in the Code pane once 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.

    Note

    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 the entire document by clicking Advanced, and then Format Document from the Edit menu, or by pressing CTRL+E, D.

    When typing 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 attempts to predict the word you are typing. In this case, you won't see the word "Console" displayed just yet, so 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 entirely up to you.

  6. 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 should be able to 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.

    ExpressConsole

    Type an open parenthesis. You'll immediately see another IntelliSense feature, the method signatures, appear as a tool tip message. In this case you can see there are 19 different signatures, and you can look through them by clicking the UP and DOWN ARROW keys.

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

    Type the message inside 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.

  8. Finish the program.

    Type or copy and paste the following code to complete the program:

    static void Main(string[] args)
    {
        Console.WriteLine("This program lists all the files in the directory:");
    
        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);
        }
        Console.ReadLine();
    }
    

    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 leave out the call to the ReadLine() method.

  9. 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

  10. 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 (Visual C# Express) section, and try out some of the language examples. If you wish 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 Application.

See Also

Tasks

How to: Create a New Visual C# Express Application

Concepts

C# Language Primer (Visual C# Express)

Other Resources

Using the Visual C# Express IDE
Visual C# Express Tips and Tricks