Messagebox.show 方法不适用于 int 变量?



我的WPF应用程序包含一个表单,该表单在加载时,将获得两个变量,其中包含当前在本地计算机上运行的某个进程的PID(procid(和进程名(procname(。

我有一个数据表,每列包含netstat -ano数据输出。现在我需要将 (procid( 或 (procname( 与数据表中包含的关联数据进行比较,如果两个变量都匹配,则存储当前索引/行号并使用该索引获取要存储为变量的关联数据行的远程 IP 地址 (remoteIp(。

但是由于某种原因,当我尝试测试变量remoteIp时,消息框没有显示或只是不显示任何内容。我的代码有问题吗?

Pageone.xaml.cs

public partial class Pageone : Page
{
public Pageone(MainWindow mainWindow)
{
InitializeComponent();
}
private string remoteIp;

private void Window_Loaded(object sender, RoutedEventArgs e)
{

//instantiate the MainWindow and assign it to the 'window' variable
var window = (MainWindow)Application.Current.MainWindow;
string procName = window.proc1;
int subprocPid = window.proc2;
string procPID = subprocPid.ToString();
MessageBox.Show(procPID);

using (Process ns = new Process())
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Protocol"),
new DataColumn("Local Address"),
new DataColumn("Foreign Address"),
new DataColumn("State"),
new DataColumn("PID"),
new DataColumn("Process Name"),
});
ProcessStartInfo psi = new ProcessStartInfo("netstat.exe", "-ano");
psi.RedirectStandardOutput = true;
psi.UseShellExecute = false;
ns.StartInfo = psi;
// Run it, and read the results
ns.Start();
using (StreamReader r = ns.StandardOutput)
{
string output = r.ReadToEnd();
ns.WaitForExit();
//Parse those results into a DataTable, polling the Process info
string[] lines = output.Split(new string[] { Environment.NewLine }, StringSplitOptions.None);

foreach (string line in lines)
{
string[] elements = line.Split(' ');
if (elements.Length < 5) continue;
if (elements.Contains("Proto")) continue;
DataRow dr = dt.NewRow();
List<string> validElements = new List<string>();
//Weed out empty elements.
foreach (string element in elements)
{
//skip blanks
if (element.Trim() == "") continue;
validElements.Add(element);
}
foreach (string element in validElements)
{
foreach (DataColumn dc in dt.Columns)
{
// fill in the buckets. Note that UDP doesn't have a state
if (dr["Protocol"].ToString() == "UDP" && dc.ColumnName == "State") continue;
if (dr[dc] == DBNull.Value)
{
dr[dc] = element;
break;
}
}
}
dr["Process Name"] = Process.GetProcessById(int.Parse(dr["PID"].ToString())).ProcessName;
dt.Rows.Add(dr);

}
foreach (DataRow row in dt.Rows)
{
int index = dt.Rows.IndexOf(row);
object cellprocPid = row["PID"];
object cellprocName = row["Process Name"];
object cellprocremoteIp = row["Foreign Address"];

if(cellprocPid.ToString() == procPID)
{
//MessageBox.Show(dt.Rows[index]["Process Name"].ToString());
//MessageBox.Show(index.ToString());
remoteIp = dt.Rows[index]["Foreign Address"].ToString();
}

}
}
}

MessageBox.Show(remoteIp);
}
}

似乎 if 语句条件不匹配,因为两个变量具有不同的进程 ID,这导致消息框不显示任何内容。