【发布时间】:2023-03-27 08:39:01
【问题描述】:
我想通过一个bat文件在c#程序中添加一些DNS记录,所以我在bat文件中写了这些行:
set servername=%1
set siteaddress=%2
"C:\Windows\System32\dnscmd.exe" %servername% /zoneadd %siteaddress% /primary /file %siteaddress%.dns
我已经用 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# 之外),一切正常。
我更改了我的 C# 代码以检查发生了什么
Process.Start(@"C:\Windows\System32\dnscmd.exe");
我仍然收到“无法识别...”错误。但我可以在“C:\Windows\System32”中看到 dnscmd.exe。 我再次更改了我的 C# 代码以检查另一件事:
Process.Start(@"C:\Windows\System32\cmd.exe");
然后将打开 CMD 窗口??? 有什么想法吗?
【问题讨论】:
-
您的应用程序编译为 32 位还是 64 位?
dnscmd.exe是否存在于C:\Windows\SysWOW64中? This might be relevant. -
“C:\Windows\SysWOW64”中没有 dnscmd.exe 路径中存在 cmd.exe 吗?我该怎么办?
-
我将“C:\Windows\System32”更改为“%windir%\Sysnative”,我的问题就解决了。但是“%windir%\Sysnative”只存在于 64 位系统中(所以我的应用程序不能在 32 位系统上运行)。是否有适用于 32 位和 64 位系统的方法?
标签: c# batch-file