Advertisement
Guest User

C# VirtualBox manager

a guest
Sep 23rd, 2012
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.00 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Diagnostics;
  5.  
  6. namespace VirtualBoxManager
  7. {
  8.     class VirtualBoxManager
  9.     {
  10.         private String VMName;
  11.         private String virtualBoxManage;
  12.  
  13.         public VirtualBoxManager(String VMName, String VirtualBoxManage)
  14.         {
  15.             this.VMName = VMName;
  16.             this.virtualBoxManage = VirtualBoxManage;
  17.         }
  18.  
  19.         private String launchVBoxManager(String args, String attendedOutput)
  20.         {
  21.             Process VBoxManage = new Process();
  22.             VBoxManage.StartInfo.CreateNoWindow = true;
  23.             VBoxManage.StartInfo.FileName = this.virtualBoxManage + "VBoxManage.exe";
  24.             VBoxManage.StartInfo.WorkingDirectory = this.virtualBoxManage;
  25.             VBoxManage.StartInfo.Arguments = args;
  26.             VBoxManage.StartInfo.UseShellExecute = false;
  27.  
  28.             //We redirect output only if the user supply any attended output
  29.             if (attendedOutput != "")
  30.             {
  31.                 VBoxManage.StartInfo.RedirectStandardOutput = true;
  32.             }
  33.             VBoxManage.Start();
  34.  
  35.             //If we are waiting for an output, we wait until the end of the output
  36.             if (attendedOutput != "")
  37.             {
  38.                 String line = "";
  39.                 while (!VBoxManage.StandardOutput.EndOfStream)
  40.                 {
  41.                     System.Windows.Forms.Application.DoEvents();
  42.                     line = VBoxManage.StandardOutput.ReadToEnd();
  43.                     if (line.Trim().StartsWith(attendedOutput))
  44.                     {
  45.                         return line.Trim();
  46.                     }
  47.                 }
  48.             }
  49.             return "-1";
  50.         }
  51.  
  52.         private String launchVBoxManager(String args)
  53.         {
  54.             return this.launchVBoxManager(args, "");
  55.         }
  56.  
  57.         public void startVM()
  58.         {
  59.             launchVBoxManager(" startvm \"" + this.VMName + "\" --type headless");
  60.         }
  61.  
  62.         public void stopVM()
  63.         {
  64.             launchVBoxManager(" controlvm \"" + this.VMName + "\" poweroff");
  65.         }
  66.  
  67.         public String ipVM()
  68.         {
  69.             //Checking IP
  70.             String ip = " guestproperty get \"" + this.VMName + "\" \"/VirtualBox/GuestInfo/Net/0/V4/IP\"";
  71.             String tmpIP = launchVBoxManager(ip, "Value");
  72.             if (tmpIP != "-1")
  73.             {
  74.                 return tmpIP.Substring(7);
  75.             }
  76.             else
  77.             {
  78.                 return "0.0.0.0";
  79.             }
  80.         }
  81.  
  82.         public Boolean statusVM(String login, String password)
  83.         {
  84.             String checkStatus = " guestcontrol \"" + this.VMName + "\" execute --image \"/bin/uname\" --username \"" + login + "\" --password \"" + password + "\" --wait-stdout";
  85.             String checkStatusResult = "Linux";
  86.             //Return the boolean result
  87.             return (launchVBoxManager(checkStatus, checkStatusResult) == checkStatusResult);
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement