先在nuget包中添加System.Management.Automation引用。
然后下面就是代码了。
using System;
using System.Collections.ObjectModel;
using System.Management.Automation;
using System.Text;
using System.Windows.Forms;
namespace GetMAC
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
StringBuilder sb = new StringBuilder();
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddCommand("Get-NetAdapter");
Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
foreach (PSObject result in PSOutput)
{
sb.Append(result.Members["PermanentAddress"].Value + "
");
}
}
textBox1.Text = sb.ToString();
}
}
}