views:

170

answers:

6

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?

+7  A: 

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
Adrian Regan
You don't need to have a console application to be able to run it from a batch file ;)
Abel
how to create the batch file to run the console application using it?
Davideg
@Davideg I'm taking a stab in the dark here, but if you're asking how to physically create the batch file, just use a text editor like notepad and save the file as either *.bat or *.cmd
Chris Thompson
A: 

You can simply create a batch file on the desktop that does the following

cd \<applicationdirectory>\bin\<release or debug depending on your build configuration>
START <application name>

All you need to do is create a .txt file on your desktop, place the commands above in the file (with the correct directories and names) and then rename the file to .bat

ok man.So if I have a test.exe file and I want to change it to test.bat file, all I want is to create test.txt file and write the directory of the test.exe above in the file then rename it to test.bat. Is that what you mean? or there is another thing to do?
Davideg
+1  A: 

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
Abel
+2  A: 

As an alternative, you may want to consider using the windows powershell instead of the regular old commandline. In the powershell you can work with arbitrary .net assemblies. In other words, you don't have to do anything in your C#/VB code to accommodate the commandline.

Angelo
+1  A: 
  1. Right click on your desktop.
  2. Choose New from the menu
  3. Choose Text Document
  4. Type a name for the text document such as "RunCSharp.bat"
  5. Double click on the new file
  6. Copy the following code into it.
  7. Choose File Save from the menu
  8. Double click on the file.
  9. The C# code in the batch file will compile and run.

.

@echo off
set csfile=%temp%\temp.cs
echo // C# code > %csfile%
echo using System; >> %csfile%
echo using System.Text; >> %csfile%
echo namespace HelloWorld >> %csfile%
echo { >> %csfile%
echo   public class HelloWorldMain >> %csfile%
echo   { >> %csfile%
echo     public static void Main() >> %csfile%
echo     { >> %csfile%
echo        Console.WriteLine("Hello world!"); >> %csfile%
echo     } >> %csfile%
echo   } >> %csfile%
echo } >> %csfile%

csc /nologo /out:%temp%\temp.exe %csfile%
%temp%\temp.exe
pause
Scott Langham
Some people go to great lengths for pain.
Matthew Whited
A: 

Yes.

  1. In Visual Studio, right-click on your C# project and choose Properties
  2. Go to the Build Events tab
  3. In the post build box, type the following:

.

set batchName=%userprofile%\desktop\runCSharp.bat
echo @echo off > %batchName%
echo "$(TargetPath)" >> %batchName%
echo pause >> %batchName%

That will create a batch file on your desktop that will run the compiled c# program.

Scott Langham