using System; using System.Collections; using System.Collections.Generic; using System.Text; using Interop.VixCOM; namespace Vestris.VMWareLib { /// /// A collection of shared folders. /// Shared folders will only be accessible inside the guest operating system if shared folders are /// enabled for the virtual machine. /// public class VMWareSharedFolderCollection : ICollection, IEnumerable, IDisposable { private IVM _vm = null; private List _sharedFolders = null; /// /// A collection of shared folders that belong to a virtual machine. /// /// Virtual machine. public VMWareSharedFolderCollection(IVM vm) { _vm = vm; } /// /// Add (create) a shared folder. /// /// The shared folder to add. public void Add(VMWareSharedFolder sharedFolder) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.AddSharedFolder( sharedFolder.ShareName, sharedFolder.HostPath, sharedFolder.Flags, callback), callback)) { job.Wait(VMWareInterop.Timeouts.AddRemoveSharedFolderTimeout); } _sharedFolders = null; } catch (Exception ex) { throw new Exception( string.Format("Failed to add shared folder: shareName=\"{0}\" hostPath=\"{1}\" flags={2}", sharedFolder.ShareName, sharedFolder.HostPath, sharedFolder.Flags), ex); } } /// /// Get shared folders. /// /// A list of shared folders. private List SharedFolders { get { if (_sharedFolders == null) { try { List sharedFolders = new List(); VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.GetNumSharedFolders(callback), callback)) { int nSharedFolders = job.Wait( Constants.VIX_PROPERTY_JOB_RESULT_SHARED_FOLDER_COUNT, VMWareInterop.Timeouts.GetSharedFoldersTimeout); for (int i = 0; i < nSharedFolders; i++) { VMWareJobCallback getSharedfolderCallback = new VMWareJobCallback(); using (VMWareJob sharedFolderJob = new VMWareJob( _vm.GetSharedFolderState(i, getSharedfolderCallback), getSharedfolderCallback)) { object[] sharedFolderProperties = { Constants.VIX_PROPERTY_JOB_RESULT_ITEM_NAME, Constants.VIX_PROPERTY_JOB_RESULT_SHARED_FOLDER_HOST, Constants.VIX_PROPERTY_JOB_RESULT_SHARED_FOLDER_FLAGS }; object[] sharedFolderPropertyValues = sharedFolderJob.Wait( sharedFolderProperties, VMWareInterop.Timeouts.GetSharedFoldersTimeout); VMWareSharedFolder sharedFolder = new VMWareSharedFolder( (string)sharedFolderPropertyValues[0], (string)sharedFolderPropertyValues[1], (int)sharedFolderPropertyValues[2]); sharedFolders.Add(sharedFolder); } } } _sharedFolders = sharedFolders; } catch (Exception ex) { throw new Exception("Failed to get shared folders", ex); } } return _sharedFolders; } } /// /// Delete all shared folders. /// public void Clear() { while (SharedFolders.Count > 0) { Remove(SharedFolders[0]); } } /// /// A function to copy shared folder objects between arrays. /// Don't use externally. /// /// Target array. /// Array index. public void CopyTo(VMWareSharedFolder[] array, int arrayIndex) { SharedFolders.CopyTo(array, arrayIndex); } /// /// Returns true if this virtual machine has the folder specified. /// /// Shared folder. /// True if the virtual machine contains the specified shared folder. public bool Contains(VMWareSharedFolder item) { return SharedFolders.Contains(item); } /// /// Delete a shared folder. /// /// Shared folder to delete. /// True if the folder was deleted. public bool Remove(VMWareSharedFolder item) { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.RemoveSharedFolder( item.ShareName, 0, callback), callback)) { job.Wait(VMWareInterop.Timeouts.AddRemoveSharedFolderTimeout); } return SharedFolders.Remove(item); } catch (Exception ex) { throw new Exception( string.Format("Failed to remove shared folder: shareName=\"{0}\"", item.ShareName), ex); } } /// /// Number of shared folders. /// public int Count { get { return SharedFolders.Count; } } /// /// Returns true if the collection is read-only. /// Shared folder collections are never read-only. /// public bool IsReadOnly { get { return false; } } /// /// A shared folder enumerator. /// /// Shared folders enumerator. IEnumerator IEnumerable.GetEnumerator() { return SharedFolders.GetEnumerator(); } /// /// A shared folder enumerator. /// /// Shared folders enumerator. IEnumerator IEnumerable.GetEnumerator() { return SharedFolders.GetEnumerator(); } /// /// Enable/disable all shared folders as a feature on a virtual machine. /// public bool Enabled { set { try { VMWareJobCallback callback = new VMWareJobCallback(); using (VMWareJob job = new VMWareJob(_vm.EnableSharedFolders(value, 0, callback), callback)) { job.Wait(VMWareInterop.Timeouts.EnableSharedFoldersTimeout); } _sharedFolders = null; } catch (Exception ex) { throw new Exception( string.Format("Failed to {0} shared folders", (value == true) ? "enable" : "disable"), ex); } } } /// /// Returns a shared folder at a given index. /// /// Shared folder index. /// A shared folder. public VMWareSharedFolder this[int index] { get { return SharedFolders[index]; } } /// /// Dispose the object. /// public void Dispose() { _sharedFolders = null; _vm = null; } } }