2014-06-25 20 views
0

我有一個小窗口應用程序調用命令提示符,然後將結果分配給richTextBox。我想將命令提示符「arp -a」的結果分配給datagridview。 我該怎麼做。如何查看arp -a「ProcessStartInfo.RedirectStandardOutput」到DATAGRIDVIEW

這裏是代碼:

private void button1_Click(object sender, EventArgs e) 
    { 
     System.Diagnostics.Process process = new System.Diagnostics.Process(); 
     process.StartInfo.UseShellExecute = false; 
     process.StartInfo.FileName = "cmd.exe"; 
     process.StartInfo.Arguments = "/C arp -a"; 
     process.StartInfo.CreateNoWindow = true; 
     process.StartInfo.RedirectStandardInput = true; 
     process.StartInfo.RedirectStandardOutput = true; 
     process.StartInfo.RedirectStandardError = true; 
     process.Start(); 
     string output = process.StandardOutput.ReadToEnd(); 
     process.Close(); 
     richTextBox1.Text = output; 

    } 

回答

0

你要 「解析」 像這樣的輸出,例如:

// add columns to your grid (could also be done in designer) 
dataGridView1.Columns.AddRange(
    new DataGridViewTextBoxColumn(), 
    new DataGridViewTextBoxColumn(), 
    new DataGridViewTextBoxColumn()); 

while (!process.StandardOutput.EndOfStream) 
{ 
    string[] values = process.StandardOutput.ReadLine().Split(new char[0], StringSplitOptions.RemoveEmptyEntries); 
    if (values.Length == 3) dataGridView1.Rows.Add(values); 
}