Creating Visual C# Solutions

Creating Visual C# Solutions

New projects are created by choosing New Project from the File menu. Visual Studio .NET displays the New Project dialog box, as shown in Figure 1-8. This dialog box gives you access to all available project types, including Database, Setup, and Deployment projects. If you have Visual Basic .NET or other languages installed, you can create new projects in those languages from this dialog box.

Figure 1-8.
The Visual Studio .NET New Project dialog box, which is used to create new projects and solutions.

All projects are created as part of a solution, which can contain multiple projects of different types. A solution ties together all the projects and files that you’re working with in an instance of the Visual Studio .NET IDE. By default, when you create a new project, the current solution is closed and a new solution is created to house your project. To override this behavior and add a new project to the current solution, select the Add To Solution radio button instead of the Close Solution radio button in the New Project dialog box.

Using Project Templates

A number of templates are available for creating new Visual C# projects. Each template will create a project skeleton, usually with an initial set of source files added to the project. Each project includes an AssemblyInfo.cs source file, which is used to configure properties for your compiled assembly.

Each Visual C# project template will provide a default name for the project. In most cases, you should override the suggested project name unless you’re happy with names like ConsoleApplication1. The New Project dialog box also allows you to specify a location for your project.

The available templates for Visual C# projects are listed here:

  • Windows Application  Creates a Windows Forms application that initially consists of a single form. The class that supports the initial form also provides the main entry point to the application.

  • Class Library  Creates a project containing a single Visual C# class, with no specified inheritance. You can use this class as a starting point to create new class libraries of any type.

  • Windows Control Library  Creates a project that you can use to develop reusable user interface controls for Windows applications. The project includes one class, which is derived from System.Windows.Forms.UserControl.

  • ASP.NET Web Application  Creates an ASP.NET project, including all the necessary files for a simple Web application. The project includes a number of files specific to Web applications that will be discussed in greater detail in Chapter 20.

  • ASP.NET Web Service  Creates a special type of Web application that uses the Simple Object Access Protocol (SOAP) to expose services that can be invoked by SOAP clients.

  • Web Control Library  Creates a project that you can use to develop reusable user interface controls for Web applications. This type of project is discussed in more detail in Chapter 20.

  • Console Application  Creates a command-line application for Windows. This type of project includes one Visual C# .NET class that includes the main entry point for the application.

  • Windows Service  Creates a Windows service application, a special type of long-running process that runs on Microsoft Windows NT, Microsoft Windows 2000, and Microsoft Windows XP. This type of application runs in its own Windows session and will continue to run even if the user logs off.

  • Empty Project  Creates a Windows project with no source files. This type of project is useful if you’re planning to add existing files to the project.

  • Empty Web Project  Creates a Web application project with no source files. This type of project is useful if you’ll be using files that already exist.

  • New Project In Existing Folder  Creates a new, empty project in an existing folder, instead of creating a new project folder.

When you create a project using the ASP.NET Web Application, ASP.NET Web Service, or Empty Web Project template, the location of a machine that’s running Microsoft Internet Information Services (IIS) 5.1 or later is specified as the project location. If your development machine has IIS installed, you can specify localhost as the project location.

Generating a New Solution

Each project template creates its own unique set of files used to compile your project. A subdirectory with the same name as the project is created to contain the project’s files; by default, its location is under My Documents\Visual Studio Projects. All project files are located in the project directory except for Web applications and Web services. For these projects, most files are uploaded into a virtual directory accessible to your Web server, with only a solution file remaining in the project directory.

The first project we’ll look at is the classic Hello World program written in Visual C#.

To create a basic HelloWorld project, open the New Project dialog box, and select the Visual C# Projects folder in the Project Types list. Next select Console Application from the Templates list, and change the project name to HelloWorld. It’s not necessary to change the default location of the project; however, you might want to make note of the location so that you can access the project later. When you’re happy with the name and location of the project, click OK, and Visual Studio .NET will create the project for you.

The files generated for the HelloWorld solution fall into two categories: files that define the solution and the project, and source files that are compiled to generate the project assembly.

Solution and Project Files

When a new project such as HelloWorld is created, it’s usually created as part of a new solution. The solution will have the same name as the project, although each can be renamed after the project is created. The project and solution files for the HelloWorld project include the following:

  • HelloWorld.suo  A hidden binary file that contains current user options for the solution

  • HelloWorld.sln  A text file that contains information about the solution

  • HelloWorld.csproj  An XML file that defines the Visual C# project

  • HelloWorld.csproj.user  An XML file that contains user-specific project information

Source Files

The source files generated for a project vary for each type of project template. The specific files that are generated will be discussed throughout this book as we examine each project type. All the project types share a few similarities, however. For example, all Visual C# projects include an AssemblyInfo.cs file, which is used to define assembly characteristics. Each project’s source files also define a default namespace that encloses all classes and other types in the project. This default namespace is initially set to the name of the project but should be changed to a more unique name if your project is to be distributed to others.



Part III: Programming Windows Forms