I want to write quick console apps and compile them with VS 2010, but if I create a .cs file, then open it in VS 2010 (by simply double-clicking on the .cs file as these are registered by VS 2010), it opens, but there is no possible way to compile it.
e.g. here is some simplistic code:
using System;
public class Hello
{
public static void Main()
{
Console.WriteLine("Hello C# World");
}
}
I can compile this fro开发者_运维技巧m the command line using csc.exe hello.cs
But, if I can edit it in VS 2010, I should (sensibly) be able to compile it from the VS 2010 interface (plus debug, step through etc). In fact, although I can open and edit, it seems that Microsoft have made it impossible to compile in this way.
Could someone with knowledge of this please explain how to compile a simple .cs file from within the VS 2010 editor please ?
You need a project and solution to compile code in Visual Studio. Simply create a new C# Console Application, edit it, and run it.
Unless you elect to save the project/solution, VS can discard the entire thing. This is the best way to whip up quick example code.
Updated
I have a number of programs that are single-.cs files myself. When I want to tinker with them I simply create a new C# project and then add the file as a link. When I'm done tinkering, I close VS and choose discard. Because the .cs file was linked and not cloned, all changes are made to the original file. The discard only destroys the [temporary] project and solution files.
When I want to create something new, I create a new C# project and start writing. When I'm done, I use File -> Save As to save just the .cs file to a location where I keep all my single-source projects. I then discard the project/solution. Note that using File -> Save As creates a link so you don't have to wait until your done to do this.
.cs files need to be part of a solution.
What you are looking for is Snippet Complier. This will allow you to compile small files very quickly. http://www.sliver.com/dotnet/SnippetCompiler/
精彩评论