在Visual Studio中运行文件的任何快速方法?

时间:2021-10-18 02:43:40

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

有没有快速方法在VS 2008中使用Main方法运行文件(.cs)?

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.

通常,您需要测试一些模型代码,Going Alt + f7(Project-> ProjectName Properties)并从下拉列表中更改Startup对象非常麻烦。

5 个解决方案

#1


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.

为了编译一个文件C#程序,我创建了一个.bat文件,在该文件中我拖放.cs文件并在.cs文件目录中获取.exe文件。

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.

您可以在Visual Studio宏中使用此.bat文件来编译活动的.cs文件并运行该应用程序。

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

这将只运行所有代码都在一个文件中的程序。如果您想快速更改项目,可以将解决方案的启动项目更改为当前选择

#2


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

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

#3


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.

那些代替模型,把它们写成单元测试。您可以快速运行它们而无需更改入口点。测试可以留待以后的更改。您可以使用Asserts和Trace Writes,而不是写入控制台。

#4


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.

我保留了一个沙盒解决方案,它有一个控制台项目和我经常使用的其他小项目类型。 Snippet Tools很不错,但通常不会像调试等整个Visual Studio一样。

#5


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

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

#1


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.

为了编译一个文件C#程序,我创建了一个.bat文件,在该文件中我拖放.cs文件并在.cs文件目录中获取.exe文件。

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.

您可以在Visual Studio宏中使用此.bat文件来编译活动的.cs文件并运行该应用程序。

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

这将只运行所有代码都在一个文件中的程序。如果您想快速更改项目,可以将解决方案的启动项目更改为当前选择

#2


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

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

#3


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.

那些代替模型,把它们写成单元测试。您可以快速运行它们而无需更改入口点。测试可以留待以后的更改。您可以使用Asserts和Trace Writes,而不是写入控制台。

#4


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.

我保留了一个沙盒解决方案,它有一个控制台项目和我经常使用的其他小项目类型。 Snippet Tools很不错,但通常不会像调试等整个Visual Studio一样。

#5


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

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