简体   繁体   English

/ t不被识别为内部或外部命令。 C#批处理脚本

[英]/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.

相关问题 C#进程cmd.exe-无法将“ hadoop”识别为内部或外部命令,可操作程序或批处理文件 - C# Process cmd.exe - 'hadoop' is not recognized as an internal or external command, operable program or batch file 获取错误“ xargs”不会被识别为内部或外部命令。 通过Windows运行iOS的Appium测试时 - Getting error 'xargs' is not recognized as an internal or external command. While running appium test for iOS through Windows “C:Program”未被识别为内部或外部命令 - 'C:Program' not recognized as an internal or external command 无法将cmd命令识别为>内部或外部命令,可操作程序或批处理文件 - cmd command is not recognized as > an internal or external command, operable program or batch file C# call command get 'C/:Program' 不被识别为内部或外部命令 - C# call command get 'C/:Program' not recognized as an internal or external command 无法将“ C:\\ MPIHello \\ bin \\ Debug”识别为内部或外部命令,可操作程序或批处理文件 - 'C:\MPIHello\bin\Debug' is not recognized as an internal or external command, operable program or batch file tftp' 不是内部或外部命令,也不是可运行的程序或批处理文件 - tftp' is not recognized as an internal or external command, operable program or batch file Windows碎片整理无法识别为内部或外部命令,可操作程序或批处理文件 - Windows defrag is not recognized as an internal or external command, operable program or batch file 任务列表未被识别为内部或外部命令 - Tasklist is not recognized as internal or external command gacutil是否不能识别为内部或外部命令? - gacutil is not recognized as an internal or external command?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM