şağıda kullanıcı girişi için bir IP MAC adresini bulmak için basit bir uygulama inşa edecek …
MacFinder adlı yeni bir Windows Forms uygulaması açın
Form ve 2 yerine metin kutularının 1 Button Açık
Sonra Explorer MacFinder proje adlı bir sınıf eklemek sağ tıklayın ve add-> sınıfı
İşte yorumlar açıklamalar ile sınıf
| C# | | ? |
| 01 |
using System; |
| 02 |
using System.Linq; |
| 03 |
using System.Text; |
| 04 |
using System.Diagnostics; |
| 05 |
using System.Windows.Forms; |
| 06 |
using System.IO; |
| 07 | |
| 08 |
class MacFinder |
| 09 |
{
|
| 10 |
public string FindMac(string ip) |
| 11 |
{
|
| 12 |
string mac = null; |
| 13 | |
| 14 |
//Παρακάτω δημιουργούμε μια διεργασία για να εκτελεστή στην γραμμή εντολών |
| 15 |
Process process = new Process(); |
| 16 |
ProcessStartInfo processStartInfo = new ProcessStartInfo(); |
| 17 |
//Το αρχείο (η εντολή που θα εκτελεστή) |
| 18 |
processStartInfo.FileName = "arp"; |
| 19 |
//Παράμετροι αρχείου (εντολής) |
| 20 |
processStartInfo.Arguments = "-a " + ip; |
| 21 |
processStartInfo.RedirectStandardInput = false; |
| 22 |
processStartInfo.RedirectStandardOutput = true; |
| 23 |
processStartInfo.UseShellExecute = false; |
| 24 | |
| 25 |
try |
| 26 |
{
|
| 27 |
//Ξεκινάμε την διεργασία μας |
| 28 |
process = Process.Start(processStartInfo); |
| 29 |
string line; |
| 30 |
line = process.StandardOutput.ReadLine(); |
| 31 |
StreamReader streamReader = process.StandardOutput; |
| 32 |
string cmdString = null; |
| 33 | |
| 34 |
//Επειδή μας δείνει 3 γραμμές διαβάζουμε μέχρι την |
| 35 |
//3η και χρήσιμη γραμμή |
| 36 |
cmdString = streamReader.ReadLine(); |
| 37 |
cmdString = streamReader.ReadLine(); |
| 38 |
cmdString = streamReader.ReadLine(); |
| 39 | |
| 40 |
//Βρίσκουμε την αρχή της MAC |
| 41 |
int first = cmdString.IndexOf('-');
|
| 42 |
int startPoint = first - 2; |
| 43 |
int macLength = (2 * 6) + 5; // 6 x 'FF' & 5 x '-' = Μήκος MAC |
| 44 | |
| 45 |
//Κόβουμε την διεύθυνση MAC |
| 46 |
mac = cmdString.Substring(startPoint, macLength); |
| 47 |
} |
| 48 |
//Εάν παρουσιαστή σφάλμα την εμφανίζουμε |
| 49 |
catch (Exception e) |
| 50 |
{
|
| 51 |
MessageBox.Show("Σφάλμα !!! Παρακαλώ ελέγξτε την διεύθυνση IP");
|
| 52 |
} |
| 53 |
//Περιμένουμε για΄την έξοδο της διεργασίας και την κλείνουμε |
| 54 |
process.WaitForExit(); |
| 55 |
process.Close(); |
| 56 |
return mac; |
| 57 | |
| 58 |
} |
| 59 |
} |
| 60 | |
| 61 |
Ve son düğmesinin click olayı aşağıdaki kodu yazın:
| C# | | copy code | | ? |
| 1 |
string ip = textBox1.Text; |
| 2 |
MacFinder macFinder = new MacFinder(); |
| 3 |
textBox2.Text = macFinder.FindMac(ip); |