问题
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.
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
.
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:\Windows\System32\cscript.exe";
process.StartInfo.WorkingDirectory = location+@"\VBS\";
process.StartInfo.Arguments = "scriptName.vbs";
process.Start();
回答1:
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:\Windows\System32\cscript.exe
(or more general %windir%\System32\cscript.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:\Windows\Sysnative\cscript.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:\Windows\System32\cscript.exe";
} else {
process.StartInfo.FileName = @"C:\Windows\Sysnative\cscript.exe";
}
See also File System Redirector
Note, a similar mechanism exists also for the Registry, see Registry Redirector
回答2:
A simple answer has arose:
In my C# application I am creating, in Visual Studio (2017) had selected my Platform target
to Prefer 32-bit
, which should perhaps better be called as "force 32-bit".
By unselecting this option, my process ran as 64-bit, both by specifying the 64-bit path as the code above, but also by just running the cscript by name.
process.StartInfo.FileName = "cscript";
Toggle this option inside Properties > Build > Prefer 32-bit
来源:https://stackoverflow.com/questions/46855084/cscript-when-called-as-a-process-in-c-sharp-only-opens-32-bit-version