-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathAllocation.xaml.cs
806 lines (549 loc) · 19.6 KB
/
Allocation.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
using IPAM_NOTE.DatabaseOperation;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.NetworkInformation;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ControlzEx.Standard;
using static IPAM_NOTE.ViewMode;
using Button = System.Web.UI.WebControls.Button;
using System.Runtime.InteropServices;
using MaterialDesignThemes.Wpf;
namespace IPAM_NOTE
{
/// <summary>
/// Allocation.xaml 的交互逻辑
/// </summary>
public partial class Allocation : Window
{
public Allocation()
{
InitializeComponent();
}
private DbClass dbClass;
private string PingAddress;
private void Allocation_OnLoaded(object sender, RoutedEventArgs e)
{
if (DataBrige.SelectMode == 0 || DataBrige.SelectedIpAddress.Count == 1)//单个分配
{
if (DataBrige.SearchType == 0)
{
NetworkBlock.Text = "所在网段:" + DataBrige.SelectSearchInfo.Network;
//如果网段IP中有*则替换为0
string tempNetwork = DataBrige.SelectSearchInfo.Network;
if (tempNetwork.IndexOf("*") != -1)
{
tempNetwork = tempNetwork.Replace("*", "0");
}
PingAddress = GetFirstThreeSegments(tempNetwork) + DataBrige.SelectIp;
AddressBlock.Text = "所选地址:" + PingAddress;
if (DataBrige.SelectSearchInfo.User == "")
{
UserTextBox.Text = "";
DescriptionTextBox.Text = "";
HostNameTextBox.Text = "";
MacTextBox.Text = "";
}
else
{
UserTextBox.Text = DataBrige.SelectSearchInfo.User;
DescriptionTextBox.Text = DataBrige.SelectSearchInfo.Description;
HostNameTextBox.Text = DataBrige.SelectSearchInfo.HostName;
MacTextBox.Text = DataBrige.SelectSearchInfo.MacAddress;
}
}
else
{
//1为指定网段搜索加载,0为指定网段全部加载
if (DataBrige.LoadType == 1)
{
NetworkBlock.Text = "所在网段:" + DataBrige.ComBoxAddressInfos[DataBrige.SelectNetwork].Network;
//如果网段IP中有*则替换为0
string tempNetwork = DataBrige.ComBoxAddressInfos[DataBrige.SelectNetwork].Network;
if (tempNetwork.IndexOf("*") != -1)
{
tempNetwork = tempNetwork.Replace("*", "0");
}
Console.WriteLine("所在网段:" + GetFirstThreeSegments(tempNetwork));
PingAddress = GetFirstThreeSegments(tempNetwork) + DataBrige.SelectIp;
AddressBlock.Text = "所选地址:" + PingAddress;
if (DataBrige.ipAddressInfos[DataBrige.SelectIndex].User == "")
{
UserTextBox.Text = "";
DescriptionTextBox.Text = "";
HostNameTextBox.Text = "";
MacTextBox.Text = "";
}
else
{
UserTextBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].User;
DescriptionTextBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].Description;
HostNameTextBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].HostName;
MacTextBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].MacAddress;
}
}
else//全部加载
{
NetworkBlock.Text = "所在网段:" + DataBrige.TempAddress.Network;
//如果网段IP中有*则替换为0
string tempNetwork = DataBrige.TempAddress.Network;
if (tempNetwork.IndexOf("*") != -1)
{
tempNetwork = tempNetwork.Replace("*", "0");
}
PingAddress = GetFirstThreeSegments(tempNetwork) + DataBrige.SelectIp;
AddressBlock.Text = "所选地址:" + PingAddress;
if (DataBrige.ipAddressInfos[DataBrige.SelectIndex].User == "")
{
UserTextBox.Text = "";
DescriptionTextBox.Text = "";
HostNameTextBox.Text = "";
MacTextBox.Text = "";
}
else
{
UserTextBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].User;
DescriptionTextBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].Description;
HostNameTextBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].HostName;
MacTextBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].MacAddress;
}
}
}
if (DataBrige.SearchType!=0)
{
PingHostNameBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].OnlineHostName;
PingMacBox.Text = DataBrige.ipAddressInfos[DataBrige.SelectIndex].OnlineMacAddress;
}
}
else//批量分配
{
//禁用无关按钮
StatusPing.IsEnabled = false;
StatusPing.IsEnabled = false;
PingButton.IsEnabled = false;
WebOpen.IsEnabled = false;
CopyHostInfo.IsEnabled = false;
HostNameTextBox.IsEnabled = false;
MacTextBox.IsEnabled = false;
if (DataBrige.SelectMode == 1)//批量分配
{
ReleaseButton.IsEnabled = false;
}
else//批量释放
{
UserTextBox.IsEnabled=false;
DescriptionTextBox.IsEnabled=false;
SaveButton.IsEnabled = false;
}
//显示网段信息
NetworkBlock.Text = "所在网段:" + DataBrige.TempAddress.Network;
string address = null;
for (int a = 0; a < DataBrige.SelectedIpAddress.Count; a++)
{
if (a < 6)
{
address += (DataBrige.SelectedIpAddress[a].ToString() + ",");
}
else
{
address = address.Remove(address.Length - 1);
address += "等" + DataBrige.SelectedIpAddress.Count.ToString() + "个地址";
break;
}
}
if (address[address.Length - 1] == ',')
{
// 删除最后一个字符
address = address.Remove(address.Length - 1);
}
AddressBlock.Text = "所选地址:" + address;
}
string dbFilePath = AppDomain.CurrentDomain.BaseDirectory + @"db\";
string dbName = "Address_database.db";
dbFilePath = dbFilePath + dbName;
dbClass = new DbClass(dbFilePath);
dbClass.OpenConnection();
//获取到的主机名和MAC与备案的不一样则显示红色
if (HostNameTextBox.Text != PingHostNameBox.Text && PingHostNameBox.Text != "N/A")
{
PingHostNameBox.Foreground = Brushes.Yellow;
PingHostNameBox.Opacity = 1;
}
if (MacTextBox.Text != PingMacBox.Text && PingMacBox.Text != "N/A")
{
PingMacBox.Foreground = Brushes.Yellow;
PingMacBox.Opacity = 1;
}
}
/// <summary>
/// 取出IP地址前三部分
/// </summary>
/// <param name="ipAddressString"></param>
/// <returns></returns>
static string GetFirstThreeSegments(string ipAddressString)
{
if (IPAddress.TryParse(ipAddressString, out IPAddress ipAddress))
{
string[] segments = ipAddress.ToString().Split('.');
if (segments.Length >= 3)
{
return $"{segments[0]}.{segments[1]}.{segments[2]}.";
}
else
{
return "不正确的IP地址";
}
}
else
{
return "不正确的IP格式";
}
}
/// <summary>
/// 保存分配
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void SaveButton_OnClick(object sender, RoutedEventArgs e)
{
if (UserTextBox.Text != "" && UserTextBox.Text != "N/A" && DescriptionTextBox.Text != "" && DescriptionTextBox.Text != "N/A")
{
string sql;
if (DataBrige.SelectMode != 0)//多选分配
{
if (DataBrige.SelectedIpAddress.Count == 1)//如果多选模式下只选了1个
{
string tableName = DataBrige.TempAddress.TableName;
int ip = DataBrige.SelectedIpAddress[0];
sql = string.Format("UPDATE {0} SET \"User\" = '{1}', \"AddressStatus\" = '1' , \"Description\" = '{2}' , \"HostName\" = '{3}', \"MacAddress\" = '{4}' WHERE Address = {5}", tableName, UserTextBox.Text, DescriptionTextBox.Text, HostNameTextBox.Text, MacTextBox.Text, ip);
int index = 0;
foreach (var item in DataBrige.ipAddressInfos)
{
if (item.Address == ip)
{
DataBrige.ipAddressInfos[index].User = UserTextBox.Text;
DataBrige.ipAddressInfos[index].Description = DescriptionTextBox.Text;
DataBrige.ipAddressInfos[index].AddressStatus = 2;
DataBrige.searchInfos[index].HostName = HostNameTextBox.Text;
DataBrige.searchInfos[index].MacAddress = MacTextBox.Text;
break;
}
index++;
}
dbClass.ExecuteQuery(sql);//单选分配时执行
}
else//正常多选
{
string tableName = DataBrige.TempAddress.TableName;
foreach (var ip in DataBrige.SelectedIpAddress)
{
sql = string.Format("UPDATE {0} SET \"User\" = '{1}', \"AddressStatus\" = '2' , \"Description\" = '{2}' , \"HostName\" = '{3}', \"MacAddress\" = '{4}' WHERE Address = {5}", tableName, UserTextBox.Text, DescriptionTextBox.Text, HostNameTextBox.Text, MacTextBox.Text, ip);
int index = 0;
foreach (var item in DataBrige.ipAddressInfos)
{
if (item.Address == ip)
{
DataBrige.ipAddressInfos[index].User = UserTextBox.Text;
DataBrige.ipAddressInfos[index].Description = DescriptionTextBox.Text;
DataBrige.ipAddressInfos[index].AddressStatus = 2;
break;
}
index++;
}
dbClass.ExecuteQuery(sql);//多选分配时执行
}
}
}
else//单选分配
{
if (DataBrige.SearchType == 0)
{
string tableName = DataBrige.SelectSearchInfo.TableName;
sql = string.Format("UPDATE {0} SET \"User\" = '{1}', \"AddressStatus\" = '2' , \"Description\" = '{2}' , \"HostName\" = '{3}', \"MacAddress\" = '{4}' WHERE Address = {5}", tableName, UserTextBox.Text, DescriptionTextBox.Text, HostNameTextBox.Text, MacTextBox.Text, DataBrige.SelectIp);
DataBrige.searchInfos[DataBrige.SelectIndex].User = UserTextBox.Text;
DataBrige.searchInfos[DataBrige.SelectIndex].Description = DescriptionTextBox.Text;
DataBrige.searchInfos[DataBrige.SelectIndex].AddressStatus = 2;
DataBrige.searchInfos[DataBrige.SelectIndex].HostName = HostNameTextBox.Text;
DataBrige.searchInfos[DataBrige.SelectIndex].MacAddress = MacTextBox.Text;
}
else
{
string tableName = DataBrige.TempAddress.TableName;
sql = string.Format("UPDATE {0} SET \"User\" = '{1}', \"AddressStatus\" = '2' , \"Description\" = '{2}' , \"HostName\" = '{3}', \"MacAddress\" = '{4}' WHERE Address = {5}", tableName, UserTextBox.Text, DescriptionTextBox.Text, HostNameTextBox.Text, MacTextBox.Text, DataBrige.SelectIp);
if (DataBrige.LoadType == 0)
{
DataBrige.ipAddressInfos[DataBrige.SelectIndex].User = UserTextBox.Text;
DataBrige.ipAddressInfos[DataBrige.SelectIndex].Description = DescriptionTextBox.Text;
DataBrige.ipAddressInfos[DataBrige.SelectIndex].AddressStatus = 2;
DataBrige.ipAddressInfos[DataBrige.SelectIndex].HostName = HostNameTextBox.Text;
DataBrige.ipAddressInfos[DataBrige.SelectIndex].MacAddress = MacTextBox.Text;
}
else
{
DataBrige.ipAddressInfos[DataBrige.SelectIndex].User = UserTextBox.Text;
DataBrige.ipAddressInfos[DataBrige.SelectIndex].Description = DescriptionTextBox.Text;
DataBrige.ipAddressInfos[DataBrige.SelectIndex].AddressStatus = 2;
DataBrige.ipAddressInfos[DataBrige.SelectIndex].HostName = HostNameTextBox.Text;
DataBrige.ipAddressInfos[DataBrige.SelectIndex].MacAddress = MacTextBox.Text;
}
}
dbClass.ExecuteQuery(sql);//单选分配时执行
}
//Console.WriteLine(sql);
this.DialogResult = true;
this.Close();
}
else
{
MessageBox.Show("请填写地址分配信息!", "信息不完整", MessageBoxButton.OK, MessageBoxImage.Information);
}
}
/// <summary>
/// 删除分配
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
/// <exception cref="NotImplementedException"></exception>
private void ReleaseButton_OnClick(object sender, RoutedEventArgs e)
{
MessageBoxResult result = MessageBox.Show("你正在删除该IP地址的分配信息,此操作不可逆!继续?", "注意", MessageBoxButton.YesNo, MessageBoxImage.Information);
if (result == MessageBoxResult.Yes)
{
string tableName;
if (DataBrige.SelectMode != 0) //多选释放
{
if (DataBrige.SelectedIpAddress.Count == 1) //如果多选模式下只选了1个
{
tableName = DataBrige.TempAddress.TableName;
int ip = DataBrige.SelectedIpAddress[0];
string sql =
string.Format(
"UPDATE {0} SET \"User\" = '', \"AddressStatus\" = 1 , \"Description\" = '' , \"HostName\" = '' , \"MacAddress\" = '' WHERE Address = {1}",
tableName, ip);
int index = 0;
foreach (var item in DataBrige.ipAddressInfos)
{
if (item.Address == ip)
{
DataBrige.ipAddressInfos[index].User = "";
DataBrige.ipAddressInfos[index].Description = "";
DataBrige.ipAddressInfos[index].AddressStatus = 1;
DataBrige.searchInfos[index].HostName = "";
DataBrige.searchInfos[index].MacAddress = "";
break;
}
index++;
}
dbClass.ExecuteQuery(sql); //单选分配时执行
}
else //正常多选
{
tableName = DataBrige.TempAddress.TableName;
foreach (var ip in DataBrige.SelectedIpAddress)
{
string sql = string.Format("UPDATE {0} SET \"User\" = '', \"AddressStatus\" = 1 , \"Description\" = '' , \"HostName\" = '' , \"MacAddress\" = '' WHERE Address = {1}", tableName, ip);
int index = 0;
foreach (var item in DataBrige.ipAddressInfos)
{
if (item.Address == ip)
{
DataBrige.ipAddressInfos[index].User = "";
DataBrige.ipAddressInfos[index].Description = "";
DataBrige.ipAddressInfos[index].AddressStatus = 1;
break;
}
index++;
}
dbClass.ExecuteQuery(sql);//多选分配时执行
}
}
}
else
{
if (DataBrige.SearchType == 0)
{
tableName = DataBrige.SelectSearchInfo.TableName;
DataBrige.searchInfos[DataBrige.SelectIndex].User = "";
DataBrige.searchInfos[DataBrige.SelectIndex].Description = "";
DataBrige.searchInfos[DataBrige.SelectIndex].HostName = "";
DataBrige.searchInfos[DataBrige.SelectIndex].MacAddress = "";
DataBrige.searchInfos[DataBrige.SelectIndex].AddressStatus = 1;
}
else
{
tableName = DataBrige.TempAddress.TableName;
DataBrige.ipAddressInfos[Convert.ToInt32(DataBrige.SelectIndex)].User = "";
DataBrige.ipAddressInfos[Convert.ToInt32(DataBrige.SelectIndex)].Description = "";
DataBrige.ipAddressInfos[Convert.ToInt32(DataBrige.SelectIndex)].HostName = "";
DataBrige.ipAddressInfos[Convert.ToInt32(DataBrige.SelectIndex)].MacAddress = "";
DataBrige.ipAddressInfos[Convert.ToInt32(DataBrige.SelectIndex)].AddressStatus = 1;
}
string sql = string.Format("UPDATE {0} SET \"User\" = '', \"AddressStatus\" = 1 , \"Description\" = '' , \"HostName\" = '' , \"MacAddress\" = '' WHERE Address = {1}", tableName, DataBrige.SelectIp);
dbClass.ExecuteQuery(sql);//单选释放
}
//Console.WriteLine("DataBrige.ipAddressInfos.Count=" + DataBrige.ipAddressInfos.Count);
//Console.WriteLine("DataBrige.SelectIp=" + DataBrige.SelectIp);
this.Close();
}
}
/// <summary>
/// 窗口关闭
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void Allocation_OnClosing(object sender, CancelEventArgs e)
{
DataBrige.SelectMode = 0;
DataBrige.SelectedIpAddress.Clear();
this.DialogResult = true;
}
private void PingButton_OnClick(object sender, RoutedEventArgs e)
{
//int lastIndex = DataBrige.TempAddress.Network.LastIndexOf('.');
string arguments = $"-t {PingAddress}";
// 创建一个新的ProcessStartInfo对象
ProcessStartInfo processStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe", // 要执行的程序是cmd.exe
Arguments = $"/k ping {arguments}", // /k参数告诉cmd执行完命令后不会自动退出
UseShellExecute = false, // 必须设置为false,否则无法重定向标准输入输出
CreateNoWindow = false // 创建一个新窗口来显示CMD窗口
};
// 创建一个新的Process对象
Process process = new Process
{
StartInfo = processStartInfo
};
// 启动进程
process.Start();
}
private void WebOpen_OnClick(object sender, RoutedEventArgs e)
{
Process.Start("http://" + PingAddress);
}
/// <summary>
/// 提取主机信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void CopyHostInfo_OnClick(object sender, RoutedEventArgs e)
{
if (PingHostNameBox.Text != "N/A" && PingHostNameBox.Text != "")
{
HostNameTextBox.Text = PingHostNameBox.Text;
}
if (PingMacBox.Text != "N/A" && PingMacBox.Text != "")
{
MacTextBox.Text = PingMacBox.Text;
}
}
/// <summary>
/// 直接PING测试
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private async void StatusPing_OnClick(object sender, RoutedEventArgs e)
{
ButtonProgressAssist.SetIsIndeterminate(StatusPing, true);
string ipAddress = PingAddress; // 要检测的IP地址
try
{
IPAddress ip = IPAddress.Parse(ipAddress);
// 获取主机名
string hostName = await GetHostNameAsync(ip);
PingHostNameBox.Text = hostName;
if (HostNameTextBox.Text != hostName && hostName != "N/A")
{
PingHostNameBox.Foreground = Brushes.DarkRed;
}
else
{
PingHostNameBox.Foreground = Brushes.AliceBlue;
}
// 获取MAC地址
string macAddress = await GetMacAddressAsync(ip);
PingMacBox.Text = macAddress;
if (MacTextBox.Text != macAddress && macAddress != "N/A")
{
PingMacBox.Foreground = Brushes.DarkRed;
}
else
{
PingHostNameBox.Foreground = Brushes.AliceBlue;
}
}
catch (Exception ex)
{
Console.WriteLine("获取主机名失败");
}
ButtonProgressAssist.SetIsIndeterminate(StatusPing, false);
if (HostNameTextBox.Text != PingHostNameBox.Text && PingHostNameBox.Text != "N/A")
{
PingHostNameBox.Foreground = Brushes.DarkRed;
}
if (MacTextBox.Text != PingMacBox.Text && PingMacBox.Text != "N/A")
{
PingMacBox.Foreground = Brushes.DarkRed;
}
}
private Task<string> GetHostNameAsync(IPAddress ipAddress)
{
return Task.Run(() =>
{
try
{
return Dns.GetHostEntry(ipAddress).HostName;
}
catch (Exception e)
{
return "N/A";
}
});
}
private Task<string> GetMacAddressAsync(IPAddress ipAddress)
{
return Task.Run(() =>
{
try
{
byte[] macAddr = new byte[6];
uint macAddrLen = (uint)macAddr.Length;
StringBuilder strAddr = new StringBuilder();
// 调用 SendARP 函数获取 MAC 地址
uint result = SendARP(BitConverter.ToUInt32(ipAddress.GetAddressBytes(), 0), 0, macAddr, ref macAddrLen);
if (result == 0)
{
for (int i = 0; i < macAddr.Length; i++)
{
strAddr.AppendFormat("{0:X2}", macAddr[i]);
if (i != macAddr.Length - 1)
{
strAddr.Append(":");
}
}
}
return strAddr.ToString();
}
catch (Exception e)
{
return "N/A";
}
});
}
[DllImport("iphlpapi.dll", SetLastError = true)]
private static extern uint SendARP(uint destIp, uint srcIp, byte[] pMacAddr, ref uint phyAddrLen);
}
}