Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/302.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中执行cmd,然后在同一窗口中执行后面的另一个命令?_C#_Process_Cmd - Fatal编程技术网

C# 如何在C中执行cmd,然后在同一窗口中执行后面的另一个命令?

C# 如何在C中执行cmd,然后在同一窗口中执行后面的另一个命令?,c#,process,cmd,C#,Process,Cmd,正确的我试图完成的是一个程序,基本上设置活动分区在一次点击,节省了工作时间和使用cmd提示符等技能 我已经查看了System.Management名称空间,但无法确定如何使用它: 所以我求助于使用CMD,我有一个用C编写的模块应用程序,基本上我想运行DISKPART,然后在CMD窗口中启动DISKPART,然后我想让它选择磁盘0,然后选择分区1,最后选择active 在CMD中这样做很好,但在应用程序中却被证明很难:我设法让它在一个窗口中用Process运行DiskPart fine。开始,然后

正确的我试图完成的是一个程序,基本上设置活动分区在一次点击,节省了工作时间和使用cmd提示符等技能

我已经查看了System.Management名称空间,但无法确定如何使用它:

所以我求助于使用CMD,我有一个用C编写的模块应用程序,基本上我想运行DISKPART,然后在CMD窗口中启动DISKPART,然后我想让它选择磁盘0,然后选择分区1,最后选择active

在CMD中这样做很好,但在应用程序中却被证明很难:我设法让它在一个窗口中用Process运行DiskPart fine。开始,然后让它打开一个新窗口并运行下一段代码,但因为新窗口没有运行DiskPart CMD,所以它不起作用>:

有什么建议吗

谢谢


Ash

引入一个延迟,比如Thread.Sleep1000,让另一个进程有时间完成第一个命令怎么样?

只要你没有对输出做出决定,你就可以在你的C应用程序中构建一个批处理文件,并通过进程启动。start

您需要生成两个文件

第一个runDiskPart.bat:

第二个myScript.dp:


显然,这些名称是完全任意的,但是/s指令需要引用第二个文件的名称。

您真正想做的是等待程序退出,然后进入下一次调用。看看这个。

经过一些搜索,我想你可以用脚本文件做你想做的事情。阅读


因此,您可以在使用必要的命令创建script.txt文件后,使用Process.Start运行diskpart/s script.txt。

这可能有点费事,因此我事先表示歉意。这是我尝试和测试过的方法,可能有一个更简单的方法,但这是从我扔代码到墙上,看看什么卡住了

TLDR代码特别适用于此问题

好的,很抱歉,这个实际上没有经过测试。这个理论上是可行的

public static void ChangeMe()
 {

 string docPath =
  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

 string path1 = docPath + "\\Test.txt";
 string path2 = docPath + "\\Test.bat";

 string[] lines =
 {
     "select disk 0",
     "clean",
     "convert gpt",
     "create partition primary size=300",
     "format quick fs=ntfs label=Windows RE tools",
     "assign letter=T"
 };
 using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.txt")))
 {

     foreach (string line in lines)
         outputFile.WriteLine(line);
 }
 string[] lines =
 {
     "diskpart /s test.txt"
 };
 using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.bat")))
 {

     foreach (string line in lines)
         outputFile.WriteLine(line);
 }
 System.Diagnostics.Process.Start(path2);
 }
public static void ChangeMe()
 {

 string docPath =
  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

 string path1 = docPath + "\\Test.txt";
 string path2 = docPath + "\\Test.bat";

 string[] lines =
 {
     "select disk 0",
     "clean",
     "convert gpt",
     "create partition primary size=300",
     "format quick fs=ntfs label=Windows RE tools",
     "assign letter=T"
 };
 using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.txt")))
 {

     foreach (string line in lines)
         outputFile.WriteLine(line);
 }
 string[] lines =
 {
     "diskpart /s test.txt"
 };
 using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.bat")))
 {

     foreach (string line in lines)
         outputFile.WriteLine(line);
 }
 System.Diagnostics.Process.Start(path2);
 }
如果您想做的事情可以在批处理文件中完成,那么可能过于复杂的解决方法是让c编写一个.bat文件并运行它。如果需要用户输入,可以将输入放入变量中,并让c将其写入文件中。这种方法需要反复试验,因为它就像用另一个木偶控制一个木偶。对于Diskpart,它有点复杂,因为您必须创建两个文件,一个是.bat文件,另一个是txt文件

下面是一个批处理文件的示例,在本例中,该函数用于windows论坛应用程序中的一个按钮,用于清除打印队列

using System.IO;
using System;

   public static void ClearPrintQueue()
    {

        //this is the path the document or in our case batch file will be placed
        string docPath =
         Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //this is the path process.start usues
        string path1 = docPath + "\\Test.bat";

        // these are the batch commands
        // remember its "", the comma separates the lines
        string[] lines =
        {
            "@echo off",
            "net stop spooler",
            "del %systemroot%\\System32\\spool\\Printers\\* /Q",
            "net start spooler",
            //this deletes the file
            "del \"%~f0\"" //do not put a comma on the last line
        };

        //this writes the string to the file
        using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.bat")))
        {
            //This writes the file line by line
            foreach (string line in lines)
                outputFile.WriteLine(line);
        }
        System.Diagnostics.Process.Start(path1);

    }
如果您需要用户输入,那么您可以尝试类似的方法

这用于将计算机IP设置为静态,但询问用户IP、网关和dns服务器是什么

你需要它来工作

public static void SetIPStatic()
    {
//These open pop up boxes which ask for user input
        string STATIC = Microsoft.VisualBasic.Interaction.InputBox("Whats the static IP?", "", "", 100, 100);
        string SUBNET = Microsoft.VisualBasic.Interaction.InputBox("Whats the Subnet?(Press enter for default)", "255.255.255.0", "", 100, 100);
        string DEFAULTGATEWAY = Microsoft.VisualBasic.Interaction.InputBox("Whats the Default gateway?", "", "", 100, 100);
        string DNS = Microsoft.VisualBasic.Interaction.InputBox("Whats the DNS server IP?(Input required, 8.8.4.4 has already been set as secondary)", "", "", 100, 100);



        //this is the path the document or in our case batch file will be placed
        string docPath =
         Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        //this is the path process.start usues
        string path1 = docPath + "\\Test.bat";

        // these are the batch commands
        // remember its "", the comma separates the lines
        string[] lines =
        {
            "SETLOCAL EnableDelayedExpansion",
            "SET adapterName=",
            "FOR /F \"tokens=* delims=:\" %%a IN ('IPCONFIG ^| FIND /I \"ETHERNET ADAPTER\"') DO (",
            "SET adapterName=%%a",
            "REM Removes \"Ethernet adapter\" from the front of the adapter name",
            "SET adapterName=!adapterName:~17!",
            "REM Removes the colon from the end of the adapter name",
            "SET adapterName=!adapterName:~0,-1!",
//the variables that were set before are used here
            "netsh interface ipv4 set address name=\"!adapterName!\" static " + STATIC + " " + STATIC + " " + DEFAULTGATEWAY,
            "netsh interface ipv4 set dns name=\"!adapterName!\" static " + DNS + " primary",
            "netsh interface ipv4 add dns name=\"!adapterName!\" 8.8.4.4 index=2",
            ")",
            "ipconfig /flushdns",
            "ipconfig /registerdns",
            ":EOF",
            "DEL \"%~f0\"",
            ""
        };

        //this writes the string to the file
        using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.bat")))
        {
            //This writes the file line by line
            foreach (string line in lines)
                outputFile.WriteLine(line);
        }
        System.Diagnostics.Process.Start(path1);

    }
就像我说的。它可能有点过于复杂,但它永远不会失败,除非我把批处理命令写错了

这是diskpart的代码。您必须了解命令提示符,才能使其正常工作。使用diskpart,您不能像这样编写脚本

diskpart
select disk 0
clean
convert gpt
create partition primary size=300
format quick fs=ntfs label=Windows RE tools
assign letter=T
这是因为diskpart会打开自己的窗口,其余命令只会在命令提示符窗口中抛出错误 所以你必须让c先用命令写一个文本文件。然后使用diskpart命令创建一个批处理文件,以调用刚才编写的文本文件

正如我一开始说的,这个实际上没有经过测试。这个理论上是可行的

public static void ChangeMe()
 {

 string docPath =
  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

 string path1 = docPath + "\\Test.txt";
 string path2 = docPath + "\\Test.bat";

 string[] lines =
 {
     "select disk 0",
     "clean",
     "convert gpt",
     "create partition primary size=300",
     "format quick fs=ntfs label=Windows RE tools",
     "assign letter=T"
 };
 using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.txt")))
 {

     foreach (string line in lines)
         outputFile.WriteLine(line);
 }
 string[] lines =
 {
     "diskpart /s test.txt"
 };
 using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.bat")))
 {

     foreach (string line in lines)
         outputFile.WriteLine(line);
 }
 System.Diagnostics.Process.Start(path2);
 }
public static void ChangeMe()
 {

 string docPath =
  Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

 string path1 = docPath + "\\Test.txt";
 string path2 = docPath + "\\Test.bat";

 string[] lines =
 {
     "select disk 0",
     "clean",
     "convert gpt",
     "create partition primary size=300",
     "format quick fs=ntfs label=Windows RE tools",
     "assign letter=T"
 };
 using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.txt")))
 {

     foreach (string line in lines)
         outputFile.WriteLine(line);
 }
 string[] lines =
 {
     "diskpart /s test.txt"
 };
 using (StreamWriter outputFile = new StreamWriter(Path.Combine(docPath, "test.bat")))
 {

     foreach (string line in lines)
         outputFile.WriteLine(line);
 }
 System.Diagnostics.Process.Start(path2);
 }

你不能将命令写入一个临时的.bat文件并用Process.Start运行它吗?批处理文件听起来很有趣,我已经设法让它启动diskpart,但问题和以前一样,我如何硬编码它下一步应该做什么?在我的屏幕上显示DISKPART>正在等待输入,我现在如何添加命令以使其工作?谢谢各位的快速回复!顺便说一句FWIW,我通过运行diskpart/找到了所有这些?从命令提示符。啊!!我看那太棒了!非常感谢。我查看了帮助,但我以前没有使用过很多DOS,也从未使用过批处理文件。这太棒了!非常感谢你!灰