0

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

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();

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.

I changed my C# code to check what happened

Process.Start(@"C:\Windows\System32\dnscmd.exe");

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");

and after that CMD windows will be opened??? any idea?

Mojtaba
  • 2,764
  • 1
  • 21
  • 24
  • Is your application compiled as 32 or 64 bit? Is the `dnscmd.exe` present in `C:\Windows\SysWOW64`? [This might be relevant](http://technet.microsoft.com/en-us/magazine/ff955767.aspx). – CodeCaster Oct 21 '12 at 10:13
  • there isn't dnscmd.exe in "C:\Windows\SysWOW64" bu cmd.exe exists in path??What can I do? – Mojtaba Oct 21 '12 at 11:07
  • I changed "C:\Windows\System32" to "%windir%\Sysnative" and my problem was solved.but "%windir%\Sysnative" exist just in 64-bit system(so my application doesn't work on 32-bit systems).Is there a way for both 32-bit and 64-bit systems? – Mojtaba Oct 21 '12 at 11:44

3 Answers3

1

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
James K
  • 4,005
  • 4
  • 20
  • 28
1

In my case I created a console application to run a batch file with some java command but was getting 'java' not recognized as an internal command error.

Took me few hours but the solution is straight forward. My server had JVM running on 64 bit so I changed the platform target to 64 bit.

x86 is 32 bit and x64 is 64 bit. Project properties are below:

enter image description here

Atul K.
  • 101
  • 2
  • 2
0

Try this code:

ProcessInfo psi= new ProcessStartInfo("cmd.exe", string.Format("/c {0} {1} {2}", General.DnsBatPath, General.DnsServerName, txtSiteAddress.Text);
psi.UseShellExecute = false;

process = Process.Start(psi);
process.WaitForExit(); 

The /c parameter says that the given command should run and then cmd.exe should exit. For details, run cmd /? in windows console.

You can also try what happens when you set ShellExecute to true. Then the process should start the same way like a file is double-clicked in explorer. The disadvantage of shell execution is that if the user changed the .BAT file default application to (for example ) notepad, the file will not be executed but opened in notepad.

If you also want to redirect the console output, look here: Executing Batch File in C#

Community
  • 1
  • 1
Carsten Schütte
  • 4,408
  • 1
  • 20
  • 24