Skip to content

Instantly share code, notes, and snippets.

@dck-jp
Last active August 2, 2016 16:20
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dck-jp/8b989a83b7df8ccab5c3 to your computer and use it in GitHub Desktop.
Save dck-jp/8b989a83b7df8ccab5c3 to your computer and use it in GitHub Desktop.
Execute C# code directly on Windows by double-clicking the source file
@echo off
SET OutputCs="TempCs.cs"
SET OutputExe="TempCs.exe"
if exist %OutputCs% goto ExecuteCs
del %OutputCs%
for /f "usebackq skip=20 delims=|" %%i in (%0) do (echo %%i>>%OutputCs%)
:ExecuteCs
pushd %~dp0
FOR /F "TOKENS=1,2,*" %%I IN ('REG QUERY "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework" /v "InstallRoot"') DO IF "%%I"=="InstallRoot" SET FrameworkPath=%%K
SET PATH="%PATH%;%FrameworkPath%v4.0.30319\;%FrameworkPath%v3.5;%FrameworkPath%v3.0;"
csc /nologo /out:%OutputExe% %OutputCs%
if %ERRORLEVEL% NEQ 0 goto ExecuteFAILURE
echo __________ Execute Compiled Cs File Inside Bat File _________________
%OutputExe%
del %OutputExe% %OutputCs%
exit
:ExecuteFAILURE
%0
exit
REM ========== Write your C# code below this line ==============
using System;
namespace Test
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello, World!!");
Console.Read();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment