问题描述
我想通过bat文件在c#程序中添加一些DNS记录,所以我已经在bat文件中写了以下几行:
I wan to add some DNS records in c# program via a bat file so I have written these lines in bat file:
set servername=%1
set siteaddress=%2
"C:\Windows\System32\dnscmd.exe" %servername% /zoneadd %siteaddress% /primary /file %siteaddress%.dns
我已经用C#编写了这些行:
and I have written these lines in C#:
Process p = new Process();
p.StartInfo.UseShellExecute = false;
p.StartInfo.WorkingDirectory = Application.StartupPath;
p.StartInfo.FileName = General.DnsBatPath;
p.StartInfo.Arguments = string.Format("{0} {1}", General.DnsServerName, txtSiteAddress.Text);
p.Start();
p.WaitForExit();
我收到此错误"dnscmd.exe无法识别为内部或外部命令...",但是当我手动运行bat文件时(在C#外部),一切正常.
I get this error "dnscmd.exe is not recognized as internal or external command..." but when I run bat file manually (outside of C#) all things are OK.
我更改了C#代码以检查发生了什么
I changed my C# code to check what happened
Process.Start(@"C:\Windows\System32\dnscmd.exe");
我仍然收到无法识别..."错误.但是我可以在"C:\ Windows \ System32"中看到dnscmd.exe.我再次更改了C#代码以检查另一件事:
I still get "not recognized ..." error.but I can see dnscmd.exe in "C:\Windows\System32".I changed my C# code again to check another thing:
Process.Start(@"C:\Windows\System32\cmd.exe");
,然后将打开CMD窗口???有什么主意吗?
and after that CMD windows will be opened???any idea?
推荐答案
在回答第二个问题时,您始终可以检查环境变量 PROCESSOR_ARCHITECTURE
以查看其是否包含数字 64
.
In answer to your second question, you can always check the environmental variable PROCESSOR_ARCHITECTURE
to see if it contains the number 64
.
set servername=%1
set siteaddress=%2
if "%PROCESSOR_ARCHITECTURE%" equ "%PROCESSOR_ARCHITECTURE:64=%" (
REM 32bit
"C:\Windows\System32\dnscmd.exe" %servername% /zoneadd %siteaddress% /primary /file %siteaddress%.dns
) else (
REM 64bit
"%windir%\Sysnative\dnscmd.exe" %servername% /zoneadd %siteaddress% /primary /file %siteaddress%.dns
)
可能更可靠的方法是从注册表中获取它:
Possibly a more reliable method is to get it from the registry:
set servername=%1
set siteaddress=%2
for /f "tokens=3" %%x in ('reg Query HKLM\Hardware\Description\System\CentralProcessor\0 /v Identifier') do (
set arch=%%x
)
if %!arch:~-2!%==64 (
set dnsPath=%windir%\Sysnative
) else (
SET dnsPath=C:\Windows\System32
)
"%dnsPath%\dnscmd.exe" %servername% /zoneadd %siteaddress% /primary /file %siteaddress%.dns
这篇关于在C#中运行bat文件会引发无法识别的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!