我正在构建网络扫描实用程序应用程序,并且在列表视图中显示字符串时遇到了一些问题。
我通过套接字请求和字节编码找到本地远程计算机的Netbios名称,当我在listview中显示结果时,域名根本不显示,但是当我导出该listview行子项目的文本时它包含.csv文件中机器的NetBios名称,它正确显示整个字符串,例如“COMPUTER1-WORKGROUP”,例如在listview中它只显示“COMPUTER1”。
在这里,您可以在windows应用程序的列表视图中看到显示:
正如您在第一行的“Nom Netbios”栏中所见,它只显示“NVRSERVEUR”
然后当我获取ListViewItem.SubItem.Text并将其写入.csv文件时,如下图所示,它显示“NVRSERVEUR-IRIS”,其中IRIS代表网络域名。
以下是我用于检索netbios计算机名称和网络域名的任务的代码:
static Task<NetworkInfosWrapper> getNetBiosNameAsyncReply(PingReply reply)
{
var tcs = new TaskCompletionSource<NetworkInfosWrapper>();
byte[] receiveBuffer = new byte[1024];
Socket requestSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
requestSocket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReceiveTimeout, 100);
EndPoint remoteEndPoint = new IPEndPoint(IPAddress.Parse(reply.Address.ToString()), 137);
IPEndPoint originEndPoint = new IPEndPoint(IPAddress.Any, 0);
requestSocket.Bind(originEndPoint);
SocketAsyncEventArgs e = new SocketAsyncEventArgs();
e.SetBuffer(NameRequest, e.Offset, NameRequest.Count());
e.RemoteEndPoint = remoteEndPoint;
e.Completed += (obj, sender) =>
{
try
{
int receivedByteCount = requestSocket.ReceiveFrom(receiveBuffer, ref remoteEndPoint);
if (receivedByteCount >= 90)
{
Encoding enc = new UTF8Encoding();
//Encoding enc = new ASCIIEncoding();
string deviceName = enc.GetString(receiveBuffer, 57, 16).Trim();
string networkName = enc.GetString(receiveBuffer, 75, 16).Trim();
tcs.SetResult(new NetworkInfosWrapper(reply,(deviceName.Trim()+" - "+networkName.Trim()).Replace(" ", "")));
}
else
{
tcs.SetResult(new NetworkInfosWrapper(reply, "N/A"));
}
}
catch
{
tcs.SetResult(new NetworkInfosWrapper(reply, "N/A"));
}
};
requestSocket.SendToAsync(e);
return tcs.Task;
}
以下是我在主扫描功能列表视图中显示的方式:
foreach (var netbiosTask in netbiosTasks)
{
if (netbiosTask.Result.reply.Status == IPStatus.Success)
{
ListViewItem lvi;
addr = IPAddress.Parse(netbiosTask.Result.reply.Address.ToString());
string mac = ScanUtils.getMacByIp(netbiosTask.Result.reply.Address.ToString());
lvi = new ListViewItem(ScanUtils.GetReverseDNS(netbiosTask.Result.reply.Address.ToString(),1000));
lvi.SubItems.Add(netbiosTask.Result.reply.Address.ToString());
lvi.SubItems.Add("UP");
lvi.SubItems.Add(netbiosTask.Result.reply.RoundtripTime.ToString() + " ms");
lvi.SubItems.Add(netbiosTask.Result.netBiosName);
lvi.SubItems.Add(mac);
lvi.SubItems.Add(ScanUtils.getMacvendor(mac));
listView.Items.Add(lvi);
if(progressBar1.Value < progressBar1.Maximum) {
progressBar1.Value = progressBar1.Value + 1;
}
if (!mStopThread)
{
break;
}
}
}
以下是将listview的内容导出为.csv文件的方法:
private void enregistrerBtn_Click(object sender, EventArgs e)
{
StringBuilder csvContent = new StringBuilder();
csvContent.AppendLine("sep=;");
csvContent.AppendLine("Hostname;Adresse IP;Netbios;Adresse Mac;Mac Vendor");
foreach (ListViewItem item in listView.Items)
{
csvContent.AppendLine(string.Format("{0};{1};{2};{3};{4}", item.SubItems[0].Text, item.SubItems[1].Text, item.SubItems[4].Text, item.SubItems[5].Text, item.SubItems[6].Text));
}
string csvPath = Path.GetDirectoryName(System.AppDomain.CurrentDomain.BaseDirectory).ToString() + @"\Resources\networkScan"+ DateTime.Now.ToString("dd-MM-yyyy-hh-mm-ss") +".csv";
File.AppendAllText(csvPath, csvContent.ToString(),Encoding.UTF8);
}
我尝试将ASCII和UTF-8编码用于csv写入和套接字响应的字节编码。 还有其他人遇到过这种问题吗?
答案 0 :(得分:0)
Trim
不会从字符串中删除空字节,只会删除空白字符。专门应用Trim
来删除空字节,如下所示:
string deviceName = enc.GetString(receiveBuffer, 57, 16).Trim('\0');
string networkName = enc.GetString(receiveBuffer, 75, 16).Trim('\0');
是否在null字节后看到字符串的部分取决于您显示它们的方式。列表视图在看到空字节时停止打印文本,用于查看CSV文件的编辑器(Excel?)似乎能够在空字节后继续。