嗯,在实际提出这个问题之前,我会简单地向你们介绍一下我要做的事情。我写了几批来安装这里的东西,而且它们工作得很好。问题是......我想在C#中编写一个与批处理相同的程序。批处理的大部分工作是调用文件并使用/ S或/ silent等参数激活它们。当然,激活Windows / Office。 但我在运行Office / Windows激活器时遇到问题。下面,您将看到我们使用的批次的结构以及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
这是我的批处理代码。它只是询问您要安装哪个版本的Office,然后激活它。或者,您可以根据需要激活它。我想用C#做同样的事情,但只使用C#。我可以创建一个方法来启动批处理文件,但是,好吧......我想学习如何使CMD命令在C#中工作。这是我的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");
}
}
每次我尝试运行项目时,Visual Studio都会给我编译错误消息。我尝试了一些事情,并尝试在论坛上寻求帮助,但没有任何帮助。我也试过了:
尝试将每个命令设置为变量,然后使方法运行它们:
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);
尝试将wole命令作为单个字符串插入:
string varCommand = "%systemroot%\system32\cscript \"C:\Program Files (x86)\Microsoft Office\Office15\OSPP.VBS\" /inpkey:XXXX";
我还尝试在文件夹之间的最后一行代码中添加更多" \" s。喜欢" C:\\ windows \\ system32",但没有任何作用。有时我会得到编译错误,有时我的程序会运行......但是当CMD窗口打开时,它会闪烁一秒钟并消失。我可以从其中一个中读到的只是"语法问题"。所以,好吧......看起来CMD并没有正确阅读我的字符串。我的意思是,我没有正确宣布它们。
你们可以帮帮我这个吗?
答案 0 :(得分:1)
您的参数和参数之间的引号需要空格。
另外,要获得更多信息:在Process.Start
行放置一个断点,获取脚本&参数,并将它们粘贴到cmd窗口中。