Managing Files

I find it difficult to keep track of source code files that are buried deep within a Visual Studio project directory. Since source code is what's important here, and projects can easily be recreated, I prefer to store my source code files separately from my projects so that I can easily delete any unneeded project folders without fear that I'll be losing something important.

If you carefully choose a name and location outside the project directory when adding a new source code file, then there's nothing to worry about. If you want to change something after the file has been created, then you need to be careful.

Avoid “Save As”

Should you need to rename a source code file, or move it to another location, avoid using the “Save As” item in the File menu. This will appear to change your file, but actually creates a new file that is not connected with your project (!!!). If you then continue to work in Visual Studio, you'll grow increasingly frustrated as none of the changes you make to your file seem to do anything when you attempt to compile and run the program. (That's because Visual Studio is still working with your original file, while you're updating the copy.)

Rename a source code file by right-clicking on the file's name in Solution Explorer and select “Rename” from the popup menu. If you need to move the file to another location, first remove the file from your project by right-clicking the file's name in Solution Explorer and select “Remove” from the popup menu. Move the file to its new location using whatever method you're most comfortable with outside of Visual Studio, then add the relocated file back into your project using the “Add > Existing item…” menu item (right-click on the Source Files folder in Solution Explorer).

Reusing Projects

Rather than creating a new project for each and every program, some students prefer to use a single project and add and remove files as needed. This can be particularly helpful if you simply want to run a source code file that you've downloaded.

Right-click on the “Source Files” folder in Solution Explorer. You'll have the option to “Add > Existing item…” in the menu that appears. Navigate to and select the file that you want to add to your project. (You can also “cut-and-paste” source code into the editor window, but that can get a bit messy.)

Avoid Multiple Main Modules

Because Visual Studio simply compiles whatever source code happens to be in your project at the time, you can only have one file with a main() module in the project when you're ready to build — otherwise, the compiler will not be able to determine the starting point for your program.

You can remove any extra files from the project (without deleting them entirely) by right-clicking on the file's name in Solution Explorer and selecting “Remove”.