简体   繁体   中英

Any quick way to run a file in Visual Studio?

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.

让自己成为SnippetCompiler ,它可以运行片段(不在VS内部,但足够接近)并且可以帮助你。

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.

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\v3.5
cd %~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
    REM Path to batch file
    Dim compileScript = "C:\dev\compileCS.bat"

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

    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

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.

Snippy ,最初由Jon Skeet(我相信潜伏在这里)并由Jason Haley进一步开发。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM