在C#中运行bat文件会引发无法识别的错误

编程入门 行业动态 更新时间:2024-10-26 02:33:33
本文介绍了在C#中运行bat文件会引发无法识别的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我想通过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文件会引发无法识别的错误

本文发布于:2023-10-27 16:11:08,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1533884.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   无法识别   文件   bat

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!