2015-07-01 60 views
0

如何通過C#.net運行PowerCli工具我不知道該怎麼做。我想運行電源cli,我想在其中運行一些命令,它必須在單擊按鈕下完成。 (如何通過C#.net運行PowerCli工具我不知道該怎麼做,我想運行電源cli,我想在其中運行一些命令,並且這一切都必須在單擊按鈕的情況下完成。)如何通過C#運行PowerCli .net

這裏是我的代碼:

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Windows.Forms; 
using System.IO; 

namespace OVF_ImportExport 
{ 
    public partial class Form1 : Form 
    { 
     string sPath = ""; 
     string sName = ""; 

     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 

     } 

     private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e) 
     { 
      //richTextBox1.Text = ""; 
      foreach (DataGridViewRow row in dataGridView1.SelectedRows) 
      { 
       sName = row.Cells[0].Value.ToString(); 
       sPath = row.Cells[1].Value.ToString(); 

       //richTextBox1.Text = richTextBox1.Text + sName + Environment.NewLine; 

      } 
     } 

     private void BtnBrowse_Click(object sender, EventArgs e) 
     { 
      fbd.ShowDialog(); 
      TxtBrowsepath.Text = fbd.SelectedPath; 
     } 

     private void BtnCreate_Click(object sender, EventArgs e) 
     { 
      richTextBox1.Text = ""; 
      StreamWriter file = new StreamWriter("Export.bat"); 
      file.WriteLine("c: "); 
      file.WriteLine("cd \\"); 
      file.WriteLine("cd Program Files "); 
      file.WriteLine("cd VMware"); 
      file.WriteLine("cd VMware OVF Tool"); 

      foreach (DataGridViewRow row in dataGridView1.SelectedRows) 
      { 
       sName = row.Cells[0].Value.ToString(); 
       sPath = row.Cells[1].Value.ToString(); 

       //richTextBox1.Text = richTextBox1.Text + Environment.NewLine + sName; 
       file.WriteLine("start ovftool.exe --powerOffSource vi://" + TxtUsername.Text + ":" + TxtPassword.Text + "@" 
       + TxtIP.Text + sPath + " " + "\"" + TxtBrowsepath.Text + "\\" + sName + "\\" 
       + sName + ".ovf" + "\"" + Environment.NewLine); 

       //Console.WriteLine("sName: " + sName + "sPath: " + sPath); 

      } 
      file.WriteLine("pause"); 
      file.Close(); 
     } 


     private void button1_Click(object sender, EventArgs e) 
     { 
      string delimiter = ","; 
      string tablename = "export"; 
      string filename = ("c:\\izaz\\test.csv"); 


      DataSet dataset = new DataSet(); 
      StreamReader sr = new StreamReader(filename); 

      dataset.Tables.Add(tablename); 
      dataset.Tables[tablename].Columns.Add("Name"); 
      dataset.Tables[tablename].Columns.Add("Path"); 
      string allData = sr.ReadToEnd(); 
      string[] rows = allData.Split("\r".ToCharArray()); 
      foreach (string r in rows) 
      { 
       string[] items = r.Split(delimiter.ToCharArray()); 
       dataset.Tables[tablename].Rows.Add(items); 
      } 
      this.dataGridView1.DataSource = dataset.Tables[0].DefaultView; 

     } 

    } 

} 

回答