问题描述
我正在运行一个VBS文件,该文件需要在64位版本的cscript上执行。在命令行上,当我调用cscript时,它将打开位于 C:\Windows\System32\cscript.exe
的64位版本,并且VBS文件可以工作
I am running a VBS file that needs to be executed on the 64-bit version of cscript. On the command line, when I call cscript, it opens the 64-bit version located at C:\Windows\System32\cscript.exe
and the VBS file works fine.
不过,我想通过C#将此VBS文件称为进程。以FileName为 cscript
来启动过程确实会打开cscript,但只会打开位于 C的32位版本:C:\Windows\SysWoW64 scriptcscript.exe
。
However, I'd like to call this VBS file through C# as a Process. Starting the process with the FileName as cscript
does open cscript, but only opens the 32-bit version, located at C:\Windows\SysWoW64\cscript.exe
.
即使我将FileName设置为专门指向cscript的64位版本,它也只会加载32位版本。
Even when I set the FileName to specifically point to the 64-bit version of cscript, it only loads the 32-bit version.
我如何强制该过程打开cscript的64位版本?
How can I force the process to open the 64-bit version of cscript?
这是我的代码,包括上面解释的64位版本文件路径:
Here is my code, including the 64-bit version file path explained above:
string location = @"C:\location";
Process process = new Process();
process.StartInfo.FileName = @"C:\Windows\System32\cscript.exe";
process.StartInfo.WorkingDirectory = location+@"\VBS\";
process.StartInfo.Arguments = "scriptName.vbs";
process.Start();
推荐答案
根据您的要求,还有另一种解决方案。
Depending on your requirements there is another solution.
某些背景:当您运行64位应用程序并尝试启动 cscript.exe
时,您调用 C:\Windows\System32\cscript.exe
(或更通用的%windir%\System32\cscript.exe
)
Some background: When you run a 64-Bit application and try to start cscript.exe
then you call C:\Windows\System32\cscript.exe
(or more general %windir%\System32\cscript.exe
)
但是,当您运行32位应用程序时,每次调用%windir%\System32\
自动重定向到%windir%\SysWOW64\
。在此目录中,您会找到所有32位DLL。此重定向由Windows内部完成,您的应用程序无法识别任何差异。
However, when you run a 32-Bit application then each call to %windir%\System32\
is automatically redirected to %windir%\SysWOW64\
. In this directory you will find all your 32-Bit DLL's. This redirection is done by Windows internally and your application does not recognize any difference.
要从32位应用程序访问%windir%\System32\
,您可以使用%windir%\Sysnative\
,即 process.StartInfo.FileName = @ C:\Windows\Sysnative\cscript.exe ;
应该可以工作,即使您将应用程序编译为32位。
In order to access %windir%\System32\
from a 32-Bit application you can use %windir%\Sysnative\
, i.e. process.StartInfo.FileName = @"C:\Windows\Sysnative\cscript.exe";
should work even if you compile your application as 32-Bit.
注意,文件夹%windir%\ Sysnative\
在64位环境中不存在,因此您可以通过 Environment.Is64BitProcess
检查运行环境,因此
Note, folder %windir%\Sysnative\
does not exist in 64-Bit environment, thus you may check your run-environment by Environment.Is64BitProcess
, so
if Environment.Is64BitProcess {
process.StartInfo.FileName = @"C:\Windows\System32\cscript.exe";
} else {
process.StartInfo.FileName = @"C:\Windows\Sysnative\cscript.exe";
}
另请参见
请注意,也存在类似的机制有关注册表,请参见
Note, a similar mechanism exists also for the Registry, see Registry Redirector
这篇关于CScript在C#中作为进程调用时仅打开32位版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!