[英]/t is not recognized as an internal or external command. C# batch script
I am trying to run a batch file in c# from the project itself. 我试图从项目本身在C#中运行批处理文件。 Whenever I run it though, I get the error: 每当我运行它时,都会出现错误:
/t is not recognized as an internal or external command.
EDIT: This batch file runs perfectly fine outside of my C# project. 编辑:此批处理文件在我的C#项目之外运行得很好。
I would like to run batch files from within my application without needing the batch files themselves. 我想从我的应用程序中运行批处理文件,而不需要批处理文件本身。 I know that would probably be preferable, but I can't figure out how to run batch files from the working directory of my project, so this is the next best thing for me. 我知道这可能是更好的选择,但我无法弄清楚如何从项目的工作目录中运行批处理文件,因此这对我而言是下一个最好的选择。
Here is my code(note, I have System.diagnostics added in my project): 这是我的代码(请注意,我在项目中添加了System.diagnostics):
private void button1_Click(object sender, EventArgs e)
{
string batTest;
batTest = @"
@ECHO CHEF WORKSTATION FIX
@ECHO VERSION: 1.00.041316
@ECHO.
@ECHO THIS WILL DELETE THE CONTENTS OF 'c:\chef'
@ECHO.
@ECHO ARE YOU SURE YOU WANT TO CONTINUE ?
@PAUSE
@set folder='C:\chef'
@IF EXIST '%folder%' (
cd /d %folder%
for /F 'delims=' %%i in ('dir /b') do (rmdir '%%i' /s/q || del '%%i' /s)
)
@ECHO.
@ECHO DELETED CONTENTS OF 'c:\chef'
@PAUSE
@ECHO.
@ECHO UPDATE GROUP POLICIES ?
@PAUSE
@GPUPDATE /FORCE
@ECHO.
@ECHO REBOOT COMPUTER TO COMPLETE FIX ?
@PAUSE
@shutdown.exe /r /t 00
";
Process.Start(@"cmd.exe", batTest);
}
This little console program (as discussed in the comments) uses the StreamWriter.WriteLine
method to write the lines of the batch file to a text file in the %temp%
folder. 这个小的控制台程序(如注释中所述)使用StreamWriter.WriteLine
方法将批处理文件的行写入%temp%
文件夹中的文本文件。 It then executes the batch script and then deletes it when finished. 然后,它执行批处理脚本,完成后将其删除。
using System.IO;
using System.Diagnostics;
namespace CreateAndRunBatchFile
{
class Program
{
static void Main(string[] args)
{
string batTest = System.Environment.GetEnvironmentVariable("TEMP") +
@"\batchfile.bat";
using (StreamWriter sw = new StreamWriter(batTest))
{
sw.WriteLine("@echo off");
sw.WriteLine("echo Batch program has started...");
sw.WriteLine("REM Add your lines of batch script code like this");
sw.WriteLine("pause");
sw.WriteLine("exit");
}
Process.Start(@"cmd.exe ", "/c " + batTest);
Process.Start(@"cmd.exe ", "/c del " + batTest);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.