1.获取计算机IP地址
System.Net的Dns类可以用来获取对方计算机的IP地址。在获取远程计算机的IP时主要通过GetHostAddresses()方法来实现。方法的参数是计算机的名称。具体代码如下:
private void button1_Click(object sender, EventArgs e)
{
string HostName=Dns.GetHostName();//获取主机名
IPAddress[] ip=new IPAddress[1];
ip = Dns.GetHostAddresses(HostName);//获取主机IP
MessageBox.Show(ip[0].ToString());
}
2.获取计算机上所有磁盘分区名称和控件大小
在System.IO命名空间下的DriveInfo类的GetDrives()方法可以用来获得计算机上的所有逻辑驱动器的名称。DriveInfo类的TotalSize属性可义获得磁盘的空间大小。主要代码如下。
private void Form1_Load(object sender, EventArgs e)
{
System.IO.DriveInfo[] drive=System.IO.DriveInfo.GetDrives();
for (int i = 0; i < drive.Length; i++)
{
comboBox1.Items.Add(drive[i].Name);
}
}
private void comboBox1_SelectedValueChanged(object sender, EventArgs e)
{
System.IO.DriveInfo[] drive = System.IO.DriveInfo.GetDrives();
for (int i = 0; i < drive.Length; i++)
{
if (comboBox1.SelectedItem.ToString() == drive[i].Name)
{
textBox1.Text = Convert.ToString(drive[i].TotalSize/ 1024);
}
}
}