gpt4 book ai didi

Windows 窗体应用程序中的 C# 字符串缺少字符

转载 作者:太空宇宙 更新时间:2023-11-03 14:59:54 24 4
gpt4 key购买 nike

我正在构建网络扫描实用程序应用程序,但在 ListView 中显示字符串时遇到了一些问题。

我通过套接字请求和字节编码找到本地远程机器的 Netbios 名称,当我在 ListView 中显示结果时,域名根本不显示,但是当我导出该 ListView 行子项的文本时在 .csv 文件中包含机器的 NetBios 名称的它会正确显示整个字符串,例如“COMPUTER1-WORKGROUP”,例如在 ListView 中时它将仅显示“COMPUTER1”。

这里可以在windows应用程序的listview中看到显示:

enter image description here

正如您在第一行的“Nom Netbios”列中看到的那样,它只显示“NVRSERVEUR”

然后当我获取 ListViewItem.SubItem.Text 并将其写入 .csv 文件时,如下图所示,它显示“NVRSERVEUR-IRIS”,其中 IRIS 代表网络域名。

enter image description here

这是我用来检索 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;
}

这是我在主扫描功能的 ListView 中显示它的方式:

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);
}

我尝试对 csv 写入和套接字响应的字节编码使用 ASCII 和 UTF-8 编码。 还有其他人遇到过此类问题吗?

最佳答案

Trim 默认情况下不会从字符串中删除空字节,只会删除空白字符。专门应用 Trim 来删除空字节,如下所示:

string deviceName = enc.GetString(receiveBuffer, 57, 16).Trim('\0');
string networkName = enc.GetString(receiveBuffer, 75, 16).Trim('\0');

您是否看到空字节之后的字符串部分取决于您如何显示它们。 ListView 在看到空字节时停止打印文本,您用来查看 CSV 文件的编辑器(Excel?)似乎能够在空字节后继续。

关于Windows 窗体应用程序中的 C# 字符串缺少字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46733247/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com