6.获取主机的IP地址
可以用.Net的DNS类来获取一个主机名或一个给定主机的IP地址。要想在程序中使用DNS类,就需要包含System.Net:
Include System.Net Reference
比如说想获取http://www.mindcracker.com/的IP地址,以下代码就会完成这个任务:
// Call DNS.GetHostName to get IPHostEntry and get the IP address list.
IPHostEntry ipEntry = DNS.GetHostByName ("www.mindcracker.com");
IPAddress [] IpAddr = ipEntry.AddressList;
for (int i = 0; i < IpAddr.Length; i++)
{
Console.WriteLine ("IP Address {0}: {1} ", i, IpAddr[i].ToString ());
}
另外,使用无参数的GetHostName可以返回本地机器的主机名:
string strHostName = DNS.GetHostName ();
然后将这个主机名作为参数传递给GetHostByName,就可以获取本地机器的IP地址信息。
7.如何调用对话信息框?
MessageBox.Show("Inavlid File", "File Open Result", MessageBox.OKCancel | MessageBox.IconHand);
8.如何调用Windows API?
在C#中调用一个API同在VB中调用API一样。我们应该知道API的DLL名称,并且使用sysimport引入它。下面这个例子显示了如何调用MessageBox API:
using System;
class callAPICls {
[sysimport(dll="user32.dll")]
public static extern int MessageBoxA(int h, string m, string c, int type);
public static int Main()
{
return MessageBoxA(0, "Hello World!", "Caption", 0);
}
}

发表评论