CScript 在 c# 中作为进程调用时仅打开 32 位版本 [英] CScript when called as a process in c# only opens 32 bit version

查看:21
本文介绍了CScript 在 c# 中作为进程调用时仅打开 32 位版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行一个需要在 64 位版本的 cscript 上执行的 VBS 文件.在命令行上,当我调用 cscript 时,它会打开位于 C:WindowsSystem32cscript.exe 的 64 位版本,并且 VBS 文件工作正常.

但是,我想通过 C# 作为进程调用这个 VBS 文件.使用 FileName as cscript 启动进程会打开 cscript,但只打开 32 位版本,位于 C:WindowsSysWoW64cscript.exe.

即使我将 FileName 设置为专门指向 64 位版本的 cscript,它也只加载 32 位版本.

如何强制进程打开 64 位版本的 cscript?

这是我的代码,包括上面解释的64位版本文件路径:

string location = @"C:location";进程进程=新进程();process.StartInfo.FileName = @"C:WindowsSystem32cscript.exe";process.StartInfo.WorkingDirectory = location+@"VBS";process.StartInfo.Arguments = "scriptName.vbs";process.Start();

解决方案

根据您的要求,还有另一种解决方案.

一些背景:当您运行 64 位应用程序并尝试启动 cscript.exe 然后调用 C:WindowsSystem32cscript.exe(或更通用的%windir%System32cscript.exe)

但是,当您运行 32 位应用程序时,对 %windir%System32 的每次调用都会自动重定向到 %windir%SysWOW64.在此目录中,您将找到所有 32 位 DLL.此重定向由 Windows 内部完成,您的应用程序无法识别任何差异.

为了从 32 位应用程序访问 %windir%System32,您可以使用 %windir%Sysnative,即 process.StartInfo.FileName = @"C:WindowsSysnativecscript.exe"; 即使您将应用程序编译为 32 位,也应该可以工作.

注意,文件夹 %windir%Sysnative 在 64 位环境中不存在,因此您可以通过 Environment.Is64BitProcess 检查您的运行环境,所以

if Environment.Is64BitProcess {process.StartInfo.FileName = @"C:WindowsSystem32cscript.exe";} 别的 {process.StartInfo.FileName = @"C:WindowsSysnativecscript.exe";}

另见文件系统重定向器

请注意,注册表也存在类似的机制,请参阅 注册表重定向器

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:WindowsSystem32cscript.exe and the VBS file works fine.

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:WindowsSysWoW64cscript.exe.

Even when I set the FileName to specifically point to the 64-bit version of cscript, it only loads the 32-bit version.

How can I force the process to open the 64-bit version of cscript?

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:WindowsSystem32cscript.exe";
process.StartInfo.WorkingDirectory = location+@"VBS";
process.StartInfo.Arguments = "scriptName.vbs";
process.Start();

解决方案

Depending on your requirements there is another solution.

Some background: When you run a 64-Bit application and try to start cscript.exe then you call C:WindowsSystem32cscript.exe (or more general %windir%System32cscript.exe)

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.

In order to access %windir%System32 from a 32-Bit application you can use %windir%Sysnative, i.e. process.StartInfo.FileName = @"C:WindowsSysnativecscript.exe"; should work even if you compile your application as 32-Bit.

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:WindowsSystem32cscript.exe";
} else {
   process.StartInfo.FileName = @"C:WindowsSysnativecscript.exe";
}

See also File System Redirector

Note, a similar mechanism exists also for the Registry, see Registry Redirector

这篇关于CScript 在 c# 中作为进程调用时仅打开 32 位版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆