A simple way to run C# code from bat file

I read an interested article demonstrated how to run a bat file with embedded C# code. I repost this example here, because the original post is written in Russian. Despite the fact that there are other more powerful solutions and frameworks to achieve the same goal (e.g. Powershell) – I think the way presented by the author, is very useful for different scenarios.


/*
@echo off && cls
set WinDirNet=%WinDir%\Microsoft.NET\Framework
IF EXIST "%WinDirNet%\v2.0.50727\csc.exe" set csc="%WinDirNet%\v2.0.50727\csc.exe"
IF EXIST "%WinDirNet%\v3.5\csc.exe" set csc="%WinDirNet%\v3.5\csc.exe"
IF EXIST "%WinDirNet%\v4.0.30319\csc.exe" set csc="%WinDirNet%\v4.0.30319\csc.exe"
%csc% /nologo /out:"%~0.exe" %0
"%~0.exe"
del "%~0.exe"
exit
*/

class HelloWorld
{

    static void Main()
    {
        System.Console.WriteLine("Hello, World!\r\nI am at " + System.Environment.Version);
        System.Console.ReadLine();
    }
}

 
 
 

3 Responses to “A simple way to run C# code from bat file”

  1. Batch script to compile pixel shaders + mixing batch&C# | Xyzzer's Dev Blog
    5. November 2011 at 23:29

    [...] play with shaders again and as I looked at that script I remembered I found some interesting code here (copied from here) one day, that allows you to have a single file with batch script and C# code. I [...]

  2. Batch script to compile pixel shaders + mixing batch & C# | Xyzzer's Dev Blog
    20. March 2012 at 00:41

    [...] play with shaders again and as I looked at that script I remembered I found some interesting code here (copied from here) one day, that allows you to have a single file with batch script and C# code. I [...]

  3. Kirill
    13. April 2012 at 10:11

    Thank you for this example :-)

Leave a Reply