Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create .bat file to run c# code?

Tags:

c#

batch-file

What I need is that: I have a c# code and I want to build it in order to create a .bat file on desktop. So when I run this .bat file from desktop, it should execute the c# code.

Is there a way to change the settings or properties of c# project before compiling in order to create a .bat file that should run this c# code?

like image 657
Davideg Avatar asked Nov 22 '25 09:11

Davideg


2 Answers

Compile you C# code into a console application.

Then run this application using the batch file.

Create an file on you desktop called batch.bat (or whatever.bat) and put the following in it:

@echo off
[full path to your application]\[application name].exe [any additional parameters]

--- ALTERNATIVE ----

In the project properties, there is the option for Build Events. In the post build command line put the following:

echo @echo off > [path to desktop batchfile].bat
echo $(TargetPath) >> [path to desktop batchfile].bat
like image 124
Adrian Regan Avatar answered Nov 24 '25 23:11

Adrian Regan


I understand your question somehow that you want to compile a C# source file using a batch program. Suppose your C# source file looks like this (save as "test.cs"):

using System;

public class Print
{
    public static void Main()
    {
        Console.WriteLine("Hurray I am printing to the CONSOLE!!");
    }
}

you can compile that as follows on the command prompt:

csc /out:test.exe test.cs

Which brings us to the batch file:

@ECHO OFF
csc /out:test.exe test.cs

Or, generalized, taking up argument nr 1 from the command line:

@ECHO OFF
csc /out:%1.exe %1
like image 35
Abel Avatar answered Nov 24 '25 23:11

Abel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!