简体   繁体   中英

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. Most of what the batches do is call up files and fire them with parameters like /S or /silent. And, of course, activate Windows/Office. But I'm having problems running the Office/Windows activators. Below, you'll see the sctructure of the batches we use and the C# program's structure as well.

@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. Or, you can just activate it if you want. I want to do the same thing with C#, but using only 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#. Here's my C# class' code.

/* 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. 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:

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. Sometimes I get compilation erros, and sometimes my program runs... but when the CMD window opens, it flashes for a second and disappears. All I could read from one of them was "syntax problem". So, well... it looks like CMD isn't reading my strings properly. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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