-4

Is there any way to run command prompt commands from within a C# application? I need the name of a computer but the only way I can access it is by typing in to the cmd prompt.

nslookup myIPAdress

Like if my ip was 134.123.12.12 I would type;

nslookup 134.123.12.12

And the value it returns after Name: is what I am after. How would I get this in a c# console application?

I've already tried using

string name1 = Environment.MachineName;
Console.WriteLine(name1);
string name2 = System.Net.Dns.GetHostName();
Console.WriteLine(name2);
string name3 = System.Net.Dns.GetHostEntry("localhost").HostName;
Console.WriteLine(name3);
string name4 =DNSLookup("134.123.12.12");
string name5 = System.Net.Dns.GetHostEntry(134.123.12.12).HostName;
Console.WriteLine(name5);

But none of these produce the correct name, they just give me the server/host name of the computer. Any ideas?

  • 9
    regarding `run command prompt commands from within a C# application` google turned up a lot of help – Jonesopolis Jul 31 '13 at 13:29
  • That value should be what you get by doing `GetHostEntry` with the IP instead of 'localhost' (like you did with `name3` above) – Joe Jul 31 '13 at 13:31
  • @Joe GetHostEtry returns the host name, which isn't the name I'm looking for. – user2428835 Jul 31 '13 at 13:37
  • That's what `nslookup` does. Can you post the specific output you're seeing and expect? That's what `Name:` is supposed to be. – Joe Jul 31 '13 at 13:38
  • If you want to execute a dos command use something like http://msdn.microsoft.com/en-us/library/system.diagnostics.process.aspx?cs-save-lang=1&cs-lang=csharp#code-snippet-2 – Jonathan Jul 31 '13 at 13:39
  • 2
    I downvoted because the title does not match the problem and it shows not enough research. – dav_i Jul 31 '13 at 13:40
  • When I run nslookup with my ip it will give me Server: *server name* Address *other IP address* BUT below that it will then give me Name:*given computer name - what I'm after* Address *My IP address* Since I can't what I need through direct coding I was hoping to just run a cmd prompt command in my console application to get the name. – user2428835 Jul 31 '13 at 13:50
  • You should be able to get what you want through direct coding. nslookup should give you the host name from DNS (that's what `nslookup` does). You aren't explaining what you expect to see instead. – Joe Jul 31 '13 at 14:10
  • I figured it out after for anyone else who was wondering. I used this code **string strCmdText;** **strCmdText = "/k nslookup 134.123.12.12";** **System.Diagnostics.Process.Start("C:/Windows/System32/cmd.exe", strCmdText);** Before I was putting /c infront of my nslookup to close the window after. /k launches it correctly though. Thanks for your suggestions though! – user2428835 Jul 31 '13 at 15:56

3 Answers3

1

I got some code that might work for you.

Here it will give you your internet name:

string name = System.Net.Dns.GetHostEntry("192.168.1.254").HostName;
Console.WriteLine(name);
Console.ReadLine();

And here is will give you your IP address:

System.Net.IPHostEntry host;
string localIP = "?";
host = System.Net.Dns.GetHostEntry(System.Net.Dns.GetHostName());
foreach (System.Net.IPAddress ip in host.AddressList)
{
    if (ip.AddressFamily == System.Net.Sockets.AddressFamily.InterNetwork)
    {
        localIP = ip.ToString();
        Console.WriteLine(localIP);
    }
}
Console.ReadLine();
return;

This might not be the best way to do it but it works!

Hope this helps!!

Dozer789
  • 1,980
  • 2
  • 23
  • 43
0

Did you try to use:

System.Net.Dns.GetHostByAddress("134.123.12.12").HostName
oGJo
  • 144
  • 8
0

This will execute whatever command you put after /C.

 string CmndTxt;
 CmndTxt = "/C your command;
 System.Diagnostics.Process.Start("CMD.exe", CmndTxt);

For example:

 string Cmndtxt;
 CmndTxt = "/C ping " + IPtextBox.Text;
 System.Diagnostics.Process.Start("CMD.exe", CmndTxt);