我正在尝试从项目本身运行c#中的批处理文件。每当我运行它时,我都会收到错误:
/t is not recognized as an internal or external command.
编辑:这个批处理文件在我的C#项目之外运行得很好。
我想从我的应用程序中运行批处理文件,而不需要批处理文件本身。我知道这可能会更好,但我无法弄清楚如何从我项目的工作目录中运行批处理文件,所以这对我来说是最好的选择。
这是我的代码(注意,我的项目中添加了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);
}
答案 0 :(得分:1)
这个小控制台程序(如评论中所讨论的)使用StreamWriter.WriteLine
方法将批处理文件的行写入%temp%
文件夹中的文本文件。然后它执行批处理脚本,然后在完成后将其删除。
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);
}
}
}