可以用.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地址信息。
有SQL Server的服务器?? 是不是得监听端口1434?
//ActiveDirectorySearch1 //Displays all computer names in an Active Directory //Written 08/26/02 - John O'Donnell - [email protected] using System; using System.DirectoryServices; namespace ActiveDirectorySearch1 { class Class1 { static void Main (string[] args) { //Note : microsoft is the name of my domain for testing purposes. DirectoryEntry entry = new DirectoryEntry("LDAP://microsoft"); DirectorySearcher mySearcher = new DirectorySearcher(entry); mySearcher.Filter = ("(objectClass=computer)"); Console.WriteLine("Listing of computers in the Active Directory"); Console.WriteLine("====================== ======================"); foreach(SearchResult resEnt in mySearcher.FindAll()) { Console.WriteLine(resEnt.GetDirectoryEntry().Name.ToString()); } Console.WriteLine("=========== End of Listing ============="); } } }
To cwj007谢谢我也查到相应的内容,但不知能否知道可以知道每个计算机上是否装有SQL server呢?
To cwj007我用给出的方法但出现错误未处理的“System.Runtime.InteropServices.COMException”类型的异常出现在 system.directoryservices.dll 中其他信息:有一个命名冲突。
参考 SQL Server Backup/Restore Through SQLDMO and C# http://www.csharphelp.com/archives2/archive345.html
SQLDMO,需要先安装sql2k的sp2,然后代码如下:cbServerList.Items.Clear();SQLDMO.ApplicationClass app = new SQLDMO.ApplicationClass(); SQLDMO.NameList list = app.ListAvailableSQLServers(); string server; for(int i=0;i<list.Count;i++){ server = list.Item(i); if(server==null)continue; cbServerList.Items.Add(server); } if(list.Count<1)cbServerList.Items.Add("localhost"); cbServerList.SelectedIndex = 0; 你试试这个
wmi如何使用啊?
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地址信息。
是不是得监听端口1434?
//Displays all computer names in an Active Directory
//Written 08/26/02 - John O'Donnell - [email protected] using System;
using System.DirectoryServices; namespace ActiveDirectorySearch1
{
class Class1
{
static void Main (string[] args)
{
//Note : microsoft is the name of my domain for testing purposes.
DirectoryEntry entry = new DirectoryEntry("LDAP://microsoft");
DirectorySearcher mySearcher = new DirectorySearcher(entry);
mySearcher.Filter = ("(objectClass=computer)");
Console.WriteLine("Listing of computers
in the Active Directory");
Console.WriteLine("====================== ======================");
foreach(SearchResult resEnt in mySearcher.FindAll())
{
Console.WriteLine(resEnt.GetDirectoryEntry().Name.ToString()); }
Console.WriteLine("=========== End of Listing =============");
}
}
}
SQL Server Backup/Restore Through SQLDMO and C#
http://www.csharphelp.com/archives2/archive345.html
SQLDMO.NameList list = app.ListAvailableSQLServers();
string server;
for(int i=0;i<list.Count;i++){
server = list.Item(i);
if(server==null)continue;
cbServerList.Items.Add(server);
}
if(list.Count<1)cbServerList.Items.Add("localhost");
cbServerList.SelectedIndex = 0;
你试试这个
不能用于初始化list变量