简体   繁体   English

在C#中执行CMD命令

[英]Execute CMD commands in C#

Well, before actually asking the question, I'll give you guys a brief description of what I'm trying to do. 好吧,在实际提出问题之前,我将简要介绍一下我要做什么。 I wrote a few batches to install stuff here, and they work pretty well. 我写了几批在这里安装东西,它们工作得很好。 The thing is... I want to write a program in C# that does the same thing as the batches. 事情是...我想用C#编写一个与批处理功能相同的程序。 Most of what the batches do is call up files and fire them with parameters like /S or /silent. 批处理的大部分工作是调用文件,并使用/ S或/ silent之类的参数来触发它们。 And, of course, activate Windows/Office. 并且,当然,请激活Windows / Office。 But I'm having problems running the Office/Windows activators. 但是我在运行Office / Windows激活器时遇到问题。 Below, you'll see the sctructure of the batches we use and the C# program's structure as well. 在下面,您将看到我们使用的批处理的结构以及C#程序的结构。

@echo off

::Office Installation
:AskOffice
set INPUT=
set /P INPUT=Do you want to install Office 2010 (1), Office 2013 (2) or skip this step (3)? %=%
If /I "%INPUT%"=="1" goto 1 
If /I "%INPUT%"=="2" goto 2
If /I "%INPUT%"=="3" goto eof
echo.
echo Invalid input & goto AskOffice

::Office 2010
:1

set INPUT=
set /P INPUT=Do you want to install Office (1) or just activate it (2)?
If /I "%INPUT%"=="1" goto instalar2010 
If /I "%INPUT%"=="2" goto windows2010

:instalar2010
echo Installing Office 2010...
"\\jamaica\sistemas$\INSTALL\~SOFTWARES\Office\Office 2010\setup.exe" /config "\\jamaica\sistemas$\INSTALL\~SOFTWARES\Office\Office 2010\ProPlus.WW\config.xml"
goto windows2010

:windows2010
if defined ProgramFiles(x86) (
    @echo You're running a x64 system...
    goto 2010x64
) else (
    @echo You're running a x86 system...
    goto 2010x86
)

:2010x86
::Office 2010 Activation (x86)
echo Activating Office 2010 (x86)...
c:\windows\system32\cscript "C:\Program Files\Microsoft Office\Office14\OSPP.VBS" /inpkey:XXXXXXXXX
c:\windows\system32\cscript "C:\Program Files\Microsoft Office\Office14\OSPP.VBS" /act
goto eof

:2010x64
::Office 2010 Activation (x64)
echo Activating Office 2010 (x64)...
c:\windows\system32\cscript "C:\Program Files (x86)\Microsoft Office\Office14\OSPP.VBS" /inpkey:XXXXXXX
c:\windows\system32\cscript "C:\Program Files (x86)\Microsoft Office\Office14\OSPP.VBS" /act
goto eof

::Office 2013
:2
set INPUT=
set /P INPUT=Do you want to install Office (1) or just activate it (2)?
If /I "%INPUT%"=="1" goto instalar2013
If /I "%INPUT%"=="2" goto windows2013

:instalar2013
echo Installing Office 2013...
"\\jamaica\sistemas$\Install\~SOFTWARES\Office\Office 2013\setup.exe" /config "\\jamaica\sistemas$\Install\~SOFTWARES\Office\Office 2013\proplus.ww\config.xml"
goto windows2013

:windows2013
if defined ProgramFiles(x86) (
    @echo You're running a x64 system...
    goto 2013x64
) else (
    @echo You're running a x86 system...
    goto 2013x86
)

:2013x86
::Office 2013 Activation (x86)
echo Activating Office 2013...
c:\windows\system32\cscript "C:\Program Files\Microsoft Office\Office15\OSPP.VBS" /inpkey:XXX
c:\windows\system32\cscript "C:\Program Files\Microsoft Office\Office15\OSPP.VBS" /act
goto eof

:2013x64
::Office 2013 Activation (x64)
echo Activating Office 2013...
c:\windows\system32\cscript "C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS" /inpkey:XXXX
c:\windows\system32\cscript "C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS" /act
goto eof

:eof

This is my batch's code. 这是我批次的代码。 All it does is ask which version of Office you'd like to install and then it activates it. 它所做的只是询问您要安装哪个版本的Office,然后将其激活。 Or, you can just activate it if you want. 或者,您可以根据需要激活它。 I want to do the same thing with C#, but using only C#. 我想对C#做同样的事情,但只使用C#。 I could just create a method to fire up the batch file, but, well... I want to learn how to make CMD commands work in C#. 我可以创建一种方法来启动批处理文件,但是,...我想学习如何使CMD命令在C#中工作。 Here's my C# class' code. 这是我的C#类的代码。

/* Office's installers' paths */
string varCaminhoOffice2010 = @"\\romenia\install$\~SOFTWARES\Office\Office 2010\setup.exe";
string varCaminhoOffice2013 = @"\\romenia\install$\~SOFTWARES\Office\Office 2013\setup.exe";

/* Local folders */
string varCaminhoOffice2010x86 = @"C:\Program Files\Microsoft Office\Office14\OSPP.VBS";
string varCaminhoOffice2010x64 = @"C:\Program Files (x86)\Microsoft Office\Office14\OSPP.VBS";
string varCaminhoOffice2013x86 = @"C:\Program Files\Microsoft Office\Office15\OSPP.VBS";
string varCaminhoOffice2013x64 = @"C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS";

/* Methods */
public void mtdAtivaOffice2010()
{
    /* Office Activation */
    if (mtdCheckArc == false) // Checking system's architecture
    {
        // x86
        System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2010x86 + "/inpkey:XXXX");
        System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2010x86 + "/act");
    }
    else
    {
        // x64
        System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2010x64 + "/inpkey:XXXX");
        System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2010x64 + "/act");
    }
}

public void mtdAtivaOffice2013()
{
    /* Office activation */
    if (mtdCheckArc == false) // Checking system's architecture
    {
        // x86
        System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2013x86 + "/inpkey:XXXX");
        System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2013x86 + "/act");
    }
    else
    {
        // x64
        System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2013x64 + "/inpkey:XXXX");
        System.Diagnostics.Process.Start("CMD.exe", "/C %systemroot%\system32\cscript" + varCaminhoOffice2013x64 + "/act");
    }
}

Everytime I try to run the project, Visual Studio gives me compilation error messages. 每当我尝试运行该项目时,Visual Studio都会向我显示编译错误消息。 I've tried a few things, and tried search the forums for help, but nothing helped me. 我尝试了一些操作,并尝试在论坛上寻求帮助,但没有任何帮助。 I tried also: 我也尝试过:

Tried setting each command as a variable and then making the method run them: 尝试将每个命令设置为变量,然后使方法运行它们:

string varCScript = @"%systemroot%\system32\cscript";
string varSerial2010 = "/inpkey:XXXX";
string varSerial2013 = "/inpkey:XXXX";
string varActivate = "/act";
System.Diagnostics.Process.Start("CMD.exe", "/C" + varCScript + varCaminhoOffice2010x86 + varSerial2010);
System.Diagnostics.Process.Start("CMD.exe", "/C" + varCScript + varCaminhoOffice2010x64 + varSerial2010);
System.Diagnostics.Process.Start("CMD.exe", "/C" + varCScript + varCaminhoOffice2013x86 + varSerial2013);
System.Diagnostics.Process.Start("CMD.exe", "/C" + varCScript + varCaminhoOffice2013x64 + varSerial2013);

Tried also inserting the wole command as a single string: 尝试还将wole命令作为单个字符串插入:

string varCommand = "%systemroot%\system32\cscript \"C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS\" /inpkey:XXXX";

I also tried adding more "\\"s to this last line of code, between folders. 我还尝试在文件夹之间的最后一行代码中添加更多“ \\”。 Like "C:\\\\windows\\\\system32", but nothing works. 类似于“ C:\\\\ windows \\\\ system32”,但没有任何效果。 Sometimes I get compilation erros, and sometimes my program runs... but when the CMD window opens, it flashes for a second and disappears. 有时我遇到编译错误,有时我的程序运行...但是当CMD窗口打开时,它闪烁一秒钟然后消失。 All I could read from one of them was "syntax problem". 我只能从其中之一读到的是“语法问题”。 So, well... it looks like CMD isn't reading my strings properly. 因此,好吧……似乎CMD无法正确读取我的字符串。 I mean, I'm not declaring them properly. 我的意思是,我没有正确声明它们。

Could you guys help me with this one? 你们能帮我这个吗?

You need spaces between your parameters, and quotes around parameters which have spaces. 您的参数之间需要空格,并在带有空格的参数周围加上引号。

Also, to get some more info: put a breakpoint on the Process.Start line, get the script & arguments, and paste them into a cmd window. 另外,要获取更多信息,请执行以下操作:在Process.Start行上放置一个断点,获取脚本和参数,然后将其粘贴到cmd窗口中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM