Batch file in c# | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Batch file in c#

Hello everybody, I'd like to know, instead of create a file.bat, i want to write the script in a c# code; and when i execute the c# application i will click on a buton and the script will execute. Can you explain me how to do that and give me an example? please. thank you

25th May 2017, 7:53 PM
Attitude
Attitude - avatar
20 Answers
+ 3
// lets take an example string strCmdText; strCmdText= "/C copy /b Image1.jpg + Archive.rar Image2.jpg"; System.Diagnostics.Process.Start("CMD.exe",strCmdText); System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.Arguments = "/C copy /b Image1.jpg + Archive.rar Image2.jpg"; process.StartInfo = startInfo; process.Start();
25th May 2017, 8:56 PM
MR Programmer
MR Programmer - avatar
+ 2
i got you-- create a batch file using stream writer class. run batch file using the process class.
25th May 2017, 8:46 PM
MR Programmer
MR Programmer - avatar
+ 1
Your code is good. But the arguments you pass to the cmd.exe process are not. Arguments do not start with the path. Have a look at /C and /K https://stackoverflow.com/questions/515309/what-does-cmd-c-mean You need to start your argument with /C or /K in order to get the rest of the text executed. You do not need to use single quotes for the text or the filename
26th May 2017, 9:36 PM
sneeze
sneeze - avatar
0
Explain yourself a bit more. What do you want to do. In what language is file.bat written and what should it do ? Do you want to create a program that can write the content of file.bat or do you want to write a program that executes file.bat
25th May 2017, 8:40 PM
sneeze
sneeze - avatar
0
in my c# code i want to put the script. i don't want to create file.bat. And when i execute the c# app, i want it to execute the script.
25th May 2017, 8:53 PM
Attitude
Attitude - avatar
0
Why should the script be in your code ? If you want to run the script using the process class you need a file.
25th May 2017, 8:57 PM
sneeze
sneeze - avatar
0
@mr programmer . That is cool, I was not aware that you could use strings as parameter for the process class
25th May 2017, 9:03 PM
sneeze
sneeze - avatar
0
i will try to create a .txt file in a folder, by using the exemple of My Programmer. thank you to all of you. I will let you know.
26th May 2017, 9:10 AM
Attitude
Attitude - avatar
0
Helloooo everybody, Here is the code that i create: When I execute the app, it open me the cmd and do nothing. I want the code create a file test.txt, and write in "Je suis amoureux". string strCmdText; strCmdText = "C:/Users/Paul/Documents/Batch echo 'je suis amoureux' >> 'test.txt'"; System.Diagnostics.Process.Start("CMD.exe", strCmdText); System.Diagnostics.Process process = new System.Diagnostics.Process(); System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo(); startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; startInfo.FileName = "cmd.exe"; startInfo.Arguments = "C:/Users/Paul/Documents/Batch echo 'je suis amoureux' >> 'test.txt'"; process.StartInfo = startInfo;
26th May 2017, 10:01 AM
Attitude
Attitude - avatar
0
To create a file and put a text in use the streamwriter class as mr programmer said. https://www.dotnetperls.com/streamwriter using (StreamWriter writer = new StreamWriter(@"c:\temp\file.txt")) { writer.Write("je suis amoureux "); }
26th May 2017, 8:57 PM
sneeze
sneeze - avatar
0
okey thank you. I will try and explore the link.
26th May 2017, 9:15 PM
Attitude
Attitude - avatar
0
Sneeze, this the way that i must put the carry? startInfo.Arguments = "/C C:/Users/Paul/Documents/Batch echo 'je suis amoureux' >> 'test.txt'";
28th May 2017, 10:49 AM
Attitude
Attitude - avatar
0
The program you are executing is cmd.exe The arguments are /C : a character to tell the following command need to be excute echo : the command that needs to be execute 'je suis amoureux' : the string that belongs to the command >> : characters to tell the output needs to be writen to a file text.txt : the path of the file "/C echo 'je suis amoureux' >> C:\Users\Paul\Documents\Batch\test.txt"; notice I moved the path the path does not have single quotes the path has backslashes
28th May 2017, 9:33 PM
sneeze
sneeze - avatar
0
Thank you. it works fine now. thank you to all of you.
29th May 2017, 5:24 PM
Attitude
Attitude - avatar
0
Hellooooo, 1- What is the difference between a script file .ps1 and a script file .bat? 2- In my code where can i mentioned that in order to execute it by powerschel and not by cmd.exe? thank you.
30th May 2017, 11:47 AM
Attitude
Attitude - avatar
0
A extension is a promiss to the user that the file contains content with a certain format. .bat file contains commands that can be executed by the command prompt a file with the extension .ps1 contain commands that can be executed with powershell. It is a promiss, so the author of the file can break the promiss. Which gives you a file with a invalid format. And also other users can rename the file and change it extension. Breaking the promiss makes the file useless
30th May 2017, 8:59 PM
sneeze
sneeze - avatar
0
startInfo.FileName = "cmd.exe"; is the name of the file that is executed. cmd.exe -> runs the command-prompt notepad.exe -> start a entity of notepad word.exe -> starts word powershell.exe -> will start powershell
30th May 2017, 9:02 PM
sneeze
sneeze - avatar
0
If i realy understand, i can execute my script in the c# code by replace cmd.exe with "Powershell.exe" !
30th May 2017, 10:11 PM
Attitude
Attitude - avatar
0
yes. That is true
30th May 2017, 10:22 PM
sneeze
sneeze - avatar
0
okey thank you so much guys. I'm planing to try this code in a ASP.net MVC app. I think it will be possible to execute powershelle.exe by clicking on a buton asp! I will put the code in a controler.cs. Is it rigth what i said?
30th May 2017, 11:20 PM
Attitude
Attitude - avatar