views:

407

answers:

5

Is there any quick way to run a file(.cs) in VS 2008 with a Main method ?

Often you'd want to test some mockup code, Going Alt+f7(Project->ProjectName Properties) and changing the Startup object from a dropdown list is quite cumbersome.

+5  A: 

Get yourself the SnippetCompiler, it's made to run snippets (not inside of VS, but close enough) and may help you.

Lucero
+2  A: 

I keep a sandbox solution around that has a console project and other tiny project types that I use frequently. Snippet Tools are nice but usually don't come with the whole Visual Studio shebang like debugging etc.

Josef
+4  A: 

What about instead of mockups, writing those as unit tests. You can run those quickly without changing entry points. And the tests could stick around for later changes. Instead of writing to the Console, you would use Asserts and Trace Writes.

Tim Hoolihan
The code is usually not tests, but fiddling. Checking how things/libraries/etc. work.
nos
Unit tests are also great for fiddling, they document exactly what you found out. I even use them for testing language features I'm not completely confident with.Regards,Sebastiaan
Sebastiaan Megens
+3  A: 

To compile one file C# programs I have created a .bat file, on which I drag and drop a .cs file and get a .exe in .cs file directory.

SET PATH=%PATH%;C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727
%~d1
cd "%~p1"
csc %1

You can use this .bat file in a Visual Studio macro to compile active .cs file and run the application.

Sub RunCS()
    If Not ActiveDocument.FullName.EndsWith(".cs") Then
        Return
    End If

    Dim compileScript = "D:\dev\compileCS.bat"

    Dim compileParams As System.Diagnostics.ProcessStartInfo
    compileParams = New ProcessStartInfo(compileScript, ActiveDocument.FullName)

    Dim compiling As System.Diagnostics.Process

    compiling = System.Diagnostics.Process.Start(compileParams)
    compiling.WaitForExit()

    Dim programFile As String
    programFile = ActiveDocument.FullName.Substring(0, ActiveDocument.FullName.Length - 3) + ".exe"

    Dim running As System.Diagnostics.Process
    running = System.Diagnostics.Process.Start(programFile)

End Sub

This will run only programs for which all code is in one file. If you want to quickly change projects instead, you can change your solution's Startup Project to Current selection

Juozas Kontvainis
+1, Yeah, I did this to create a C# scripting extension.
John Gietzen
A: 

Snippy, originally by Jon Skeet (who lurks on here I believe) and further developed by Jason Haley.

locster