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