public static string ConvertVMStateToString(VMVMState vmstate) { string sVMState = ""; switch (vmstate) { case VMVMState.vmVMState_Invalid: sVMState = "Invalid"; break; case VMVMState.vmVMState_Paused: sVMState = "Paused"; break; case VMVMState.vmVMState_Restoring: sVMState = "Restoring"; break; case VMVMState.vmVMState_Running: sVMState = "Running"; break; case VMVMState.vmVMState_Saved: sVMState = "Saved"; break; case VMVMState.vmVMState_Saving: sVMState = "Saving"; break; case VMVMState.vmVMState_TurnedOff: sVMState = "Turned Off"; break; case VMVMState.vmVMState_TurningOff: sVMState = "Turning Off"; break; case VMVMState.vmVMState_TurningOn: sVMState = "Turning On"; break; default: sVMState = "Unknown"; break; } return(sVMState); }
private void btnRemove_Click(object sender, System.EventArgs e) { btnRemove.Enabled = false; if (MessageBox.Show("Configuration file and all VHD files in the VM folder might be deleted! Are you sure?", "Confirmation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No) { return; } // Get information from config.xml // XmlDocument xmlDoc = new XmlDocument(); // // try // { // // Load config.xml // xmlDoc.Load(CONFIGFILE); // } // catch(XmlException xmlEx) // Handle the Xml exceptions here. // { // Console.WriteLine("{0}", xmlEx.Message); // } // catch(Exception ex) // Handle the generic exceptions. here // { // Console.WriteLine("{0}", ex.Message); // } // If this VM hasn't been added, add it to config.cml bool existVM = false; int VMIndex = -1; int tabIndex = 0; XmlNodeList nodeList = frmMain.xmlConfig.SelectNodes("//VirtualServer"); for (int i = 0; i < nodeList.Count; i++) { if (nodeList[i].SelectSingleNode("ServerAddress").InnerText == txtServerAddress.Text && nodeList[i].SelectSingleNode("ServerDisplayName").InnerText == lstVM.Items[lstVM.SelectedIndex].ToString()) { existVM = true; tabIndex = i; } } // Find out the VM index try { if (myVS.VirtualMachines.Count > 0) { for (int i = 1; i <= myVS.VirtualMachines.Count; i++) { if (myVS.VirtualMachines[i].Name == lstVM.Items[lstVM.SelectedIndex].ToString()) { VMIndex = i; break; } } } } catch { MessageBox.Show("Cannot connect to Virtual Server."); } // Process if (existVM) { // Stop and Remove it from VS // Get State of this VM. If it is running, turn it off. VMVMState thisVMState = myVS.VirtualMachines[VMIndex].State; if (thisVMState != VMVMState.vmVMState_TurnedOff && thisVMState != VMVMState.vmVMState_Saved) { myVS.VirtualMachines[VMIndex].TurnOff(); while (myVS.VirtualMachines[VMIndex].State != VMVMState.vmVMState_TurnedOff) { Thread.Sleep(100); } } // Remove from tabMain. frmMainRemove.removeTab(tabIndex); // Update Config nodeList[tabIndex].ParentNode.RemoveChild(nodeList[tabIndex]); } // UnregisterVirtualMachine will remove the VM folder permanently. // .vhd stored at another location will not be deleted. // Save it first might be a good idea. string ConfFile = myVS.VirtualMachines[VMIndex].File; string ConfFilePath = ConfFile.Substring(0, (ConfFile.IndexOf(myVS.VirtualMachines[VMIndex].Name + ".vmc")) - 1); myVS.UnregisterVirtualMachine(myVS.VirtualMachines[VMIndex]); // // xmlDoc.PreserveWhitespace = true; // XmlTextWriter wrtr = new XmlTextWriter("config.xml", Encoding.UTF8); // wrtr.Formatting = Formatting.Indented ; // xmlDoc.WriteTo(wrtr); // wrtr.Close(); // Reload config.xml to reorder tabindex // Question: Why do I have to save XML first and reload? // xmlDoc.Load(CONFIGFILE); nodeList = frmMain.xmlConfig.SelectNodes("//VirtualServer"); for (int i = 0; i < nodeList.Count; i++) { nodeList[i].SelectSingleNode("TabIndex").InnerText = i.ToString(); } frmMain.conf.ConfigUpdated = true; //xmlDoc.Save(CONFIGFILE); if (frmMainRemove.tabMain.TabPages.Count > 0) { frmMainRemove.touchTab(); } btnConnect_Click(this, new System.EventArgs()); }