Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Windows batch file containing self-compiling C# code, automatic .NET path detection. Compiles itself to "filename.cmd.exe" with CSC.EXE from .NET installation. If already compiled, launches the *.exe.
/* This section is run in batch file mode
@echo off
REM Based on
if not exist %0.exe (
for /f "usebackq tokens=1,3 delims= " %%A in (`REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\NET Framework Setup\NDP" /s /e /f InstallPath 2^>nul ^| findstr InstallPath ^| sort /R`) do (
set FrameworkDir=%%B
goto :found
%FrameworkDir%\csc.exe /nologo /out:%0.exe /Reference:%FrameworkDir%\System.Net.dll "%0"
goto end
using System;
static class Program
static void Main(string[] argv)
Console.Write("C# says: ");
if (argv.Length > 0)
REM ** Run the compiled program
%0.exe "Hello world!"
REM */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.