public static void SearchVM(ref List <VirtualMachine> VMList) { var RoamingAppdata = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); var UserProfile = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile); var Version7Folder = Path.Combine(UserProfile, "Virtual Machines"); var VMFolder = Path.Combine(RoamingAppdata, @"Microsoft\Virtual PC\Virtual Machines"); string[] Files; //Virtual 2004 2007 if (Directory.Exists(VMFolder)) { Files = Directory.GetFiles(VMFolder); foreach (string lnkFile in Files) { if (Path.GetExtension(lnkFile) == ".lnk") { var Shell = new IWshRuntimeLibrary.WshShell(); IWshRuntimeLibrary.WshShortcut Shortcut = Shell.CreateShortcut(lnkFile); var Target = Shortcut.TargetPath; if (File.Exists(Target) && (Path.GetExtension(Target) == ".vmc")) { var VM = new VirtualPC_VM(Target); VMList.Add(VM); } } } } //Windows Virtual PC (Version7) if (Directory.Exists(Version7Folder)) { Files = Directory.GetFiles(Version7Folder); foreach (string vmcx in Files) { if (Path.GetExtension(vmcx) == ".vmcx") { var XML = new VirtualPC_XML(vmcx); var Target = XML.ReadValue(@"vm_description/vmc_path"); if (File.Exists(Target) && (Path.GetExtension(Target) == ".vmc")) { var VM = new VirtualPC_VM(Target); VMList.Add(VM); } } } } }
//按照文件后缀判断格式并打开 private void DetectAndOpen(string file) { VirtualMachine VM = null; switch (Path.GetExtension(file)) { case ".vmx": VM = new VMwareVM(file); break; case ".vbox": VM = new VBoxVM(file); break; case ".vmc": VM = new VirtualPC_VM(file); break; } if (VM != null) { OpenVM(VM); } }