How to get local machine name and IP address?

本文介绍了一种方法来获取当前计算机的NetBIOS名称及IP地址。通过使用Environment.MachineName可以得到本地主机名,而通过Dns.GetHostEntry结合Dns.GetHostName则能够获取到所有分配给该主机的IP地址。文章还提供了一个实用类Network,其中包含用于查找特定偏好设置下的IP地址的方法。

THANKS nick.fletcher@iinet.net.au and Dave Sexton, your notes give me a lot of help.

 

Environment.MachineName will return the local NetBios name as a string

you can also use:

System.Net.dns.GetHostName();

Getting the IP addresses is a little more tricky - as there can be more
than one per host name:

public static class Network

{

public static string GetMachineName()

{

//get domain and machine name;

//return System.Net.Dns.GetHostName();

//just get the local machine's NetBIOS name.

return Environment.MachineName;

}

public static string GetUserName()

{

return HttpContext.Current.User.Identity.Name;

}

#region DNS

public static IPAddress FindIPAddress(bool localPreference)

{

return FindIPAddress(Dns.GetHostEntry(Dns.GetHostName()),

localPreference);

}

public static IPAddress FindIPAddress(IPHostEntry host, bool

localPreference)

{

if (host == null)

throw new ArgumentNullException("host");

if (host.AddressList.Length == 1)

return host.AddressList[0];

else

{

foreach (System.Net.IPAddress address in host.AddressList)

{

bool local = IsLocal(address);

if (local && localPreference)

return address;

else if (!local && !localPreference)

return address;

}

return host.AddressList[0];

}

}

public static bool IsLocal(IPAddress address)

{

if (address == null)

throw new ArgumentNullException("address");

byte[] addr = address.GetAddressBytes();

return addr[0] == 10

|| (addr[0] == 192 && addr[1] == 168)

|| (addr[0] == 172 && addr[1] >= 16 && addr[1] <= 31);

}

#endregion

}

 

When using PyQt5 with a QTableWidget, if you want to retrieve the name of the selected column, you can use the `currentItem()` function to access the current item in the table and then its `data()` method along with `column` attribute to obtain the data and column index. Here's an example: ```python from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QTableWidget, QTableWidgetItem class TableSelectionExample(QWidget): def __init__(self): super().__init__() self.table = QTableWidget() self.init_table() layout = QVBoxLayout() layout.addWidget(self.table) self.setLayout(layout) def init_table(self): headers = ["Column 1", "Column 2", "Column 3"] self.table.setColumnCount(len(headers)) self.table.setHorizontalHeaderLabels(headers) for i in range(10): # Example data row = [] for j, header in enumerate(headers): item = QTableWidgetItem(f"Row {i}, Column {j+1}") row.append(item) self.table.setRow(i, row) def select_and_get_column_name(self): current_item = self.table.currentItem() if current_item: column_index = current_item.column() column_name = self.table.horizontalHeaderItem(column_index).text() return column_name if __name__ == "__main__": app = QApplication([]) ex = TableSelectionExample() ex.show() selected_column = ex.select_and_get_column_name() print(f"The selected column name is: {selected_column}") # Replace this line with your desired action after getting the column name app.exec_() ``` In this code snippet, we create a simple QTableWidget, fill it with some sample data, and define a method `select_and_get_column_name()` that retrieves the selected column name when a cell is clicked or selected. Remember to install and properly import the necessary modules for PyQt5, as shown in the provided error message[^1]. If you're still encountering issues with importing, make sure you have PyQt5 installed correctly and the environment is set up accordingly.
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值