public string[] GetVmdksFullName(string folderName, VimClientlContext ctx) { List <string> stringList = new List <string>(); string str1 = VimUtils.GetVolumeName(this.Name) + folderName; HostDatastoreBrowserSearchResults[] browserSearchResults1 = this.GetBrowserSearchResults(str1, ctx); if (browserSearchResults1 != null && browserSearchResults1.Length != 0) { foreach (HostDatastoreBrowserSearchResults browserSearchResults2 in browserSearchResults1) { string strA = browserSearchResults2.folderPath.TrimEnd('/'); if (browserSearchResults2.file != null && string.Compare(strA, str1, true) == 0) { foreach (VimApi.FileInfo fileInfo in browserSearchResults2.file) { string pattern = "w*-[0-9][0-9][0-9][0-9][0-9][0-9]w*"; if (!Regex.Match(fileInfo.path, pattern, RegexOptions.IgnoreCase).Success) { string str2 = browserSearchResults2.folderPath + fileInfo.path; if (!stringList.Contains(str2)) { stringList.Add(browserSearchResults2.folderPath + fileInfo.path); } } } break; } } } return(stringList.ToArray()); }
public bool FileExist(string fullName, VimClientlContext ctx) { fullName = fullName.Replace("\\", "/"); string str1 = fullName.Substring(0, fullName.LastIndexOf("/")); string str2 = fullName.Substring(fullName.LastIndexOf("/") + 1); HostDatastoreBrowserSearchResults[] browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])null; VCTask task = new VCTask(this.VcService, this.VcService.Service.SearchDatastoreSubFolders_Task(this.GetManagedObjects(new string[1] { "browser" })[0], this.GetVolumeName(this.Name) + str1.Trim("/".ToCharArray()), new HostDatastoreBrowserSearchSpec() { matchPattern = new string[1] { str2 }, searchCaseInsensitive = true, searchCaseInsensitiveSpecified = true, sortFoldersFirst = true, sortFoldersFirstSpecified = true })); string op = "Browse Datastore"; VimClientlContext rstate = ctx; task.WaitForResult(op, rstate); string[] properties1 = new string[1] { "info.result" }; Dictionary <string, object> properties2 = task.GetProperties(properties1); if (properties2.ContainsKey("info.result")) { browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])properties2["info.result"]; } return(browserSearchResultsArray != null && browserSearchResultsArray.Length != 0 && browserSearchResultsArray[0].file != null); }
public void DeleteFile(string filePath, VimClientlContext ctx) { filePath = filePath.Replace("\\", "/"); this.VcService.Service.DeleteFile(this.GetManagedObjects(new string[1] { "browser" })[0], filePath); }
public VimDatastoreItem[] FindDatastoreItemsInFolder(string folderName, VimClientlContext ctx) { if (this.snapshotDiskPattern == null) { this.snapshotDiskPattern = new Regex("w*-[0-9][0-9][0-9][0-9][0-9][0-9]w*"); } List <VimDatastoreItem> vimDatastoreItemList = new List <VimDatastoreItem>(); HostDatastoreBrowserSearchResults datastoreSearchResults = this.GetDatastoreSearchResults(VCService.GetVolumeName(this.Name) + folderName, ctx); if (datastoreSearchResults != null && datastoreSearchResults.file != null && datastoreSearchResults.file.Length != 0) { foreach (VimApi.FileInfo fileInfo in datastoreSearchResults.file) { VimDatastoreItem vimDatastoreItem = new VimDatastoreItem() { Name = fileInfo.path, Size = fileInfo.fileSize }; if (fileInfo is FolderFileInfo) { vimDatastoreItem.Type = "FOLDER"; } else if (fileInfo is VmDiskFileInfo && !this.snapshotDiskPattern.IsMatch(fileInfo.path)) { vimDatastoreItem.Type = "VMDK"; } else { continue; } vimDatastoreItemList.Add(vimDatastoreItem); } } return(vimDatastoreItemList.ToArray()); }
public Dictionary <string, VimDatastoreItem[]> FindDatastoreItems(VimClientlContext ctx) { if (this.snapshotDiskPattern == null) { this.snapshotDiskPattern = new Regex("w*-[0-9][0-9][0-9][0-9][0-9][0-9]w*"); } Dictionary <string, VimDatastoreItem[]> dictionary = new Dictionary <string, VimDatastoreItem[]>((IEqualityComparer <string>)StringComparer.CurrentCultureIgnoreCase); HostDatastoreBrowserSearchResults[] datastoreSearchResults = this.GetDatastoreSearchResults(ctx); int length = VCService.GetVolumeName(this.Name).Length; if (datastoreSearchResults != null && datastoreSearchResults.Length != 0) { foreach (HostDatastoreBrowserSearchResults browserSearchResults in datastoreSearchResults) { string str = browserSearchResults.folderPath.TrimEnd('/'); if (length > str.Length) { length = str.Length; } string key = str.Remove(0, length); List <VimDatastoreItem> vimDatastoreItemList = new List <VimDatastoreItem>(); if (browserSearchResults.file != null) { foreach (VimApi.FileInfo fileInfo in browserSearchResults.file) { VimDatastoreItem vimDatastoreItem = new VimDatastoreItem() { Name = fileInfo.path, Size = fileInfo.fileSize }; if (fileInfo is FolderFileInfo) { vimDatastoreItem.Type = "FOLDER"; } else if (fileInfo is VmDiskFileInfo && !this.snapshotDiskPattern.IsMatch(fileInfo.path)) { vimDatastoreItem.Type = "VMDK"; } else { continue; } vimDatastoreItemList.Add(vimDatastoreItem); } } dictionary.Add(key, vimDatastoreItemList.ToArray()); } } return(dictionary); }
private HostDatastoreBrowserSearchResults[] GetDatastoreSearchResults(VimClientlContext ctx) { string volumeName = VCService.GetVolumeName(this.Name); HostDatastoreBrowserSearchResults[] browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])null; ManagedObjectReference[] managedObjects = this.GetManagedObjects(new string[1] { "browser" }); HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); searchSpec.matchPattern = new string[1] { "*.vmdk" }; searchSpec.searchCaseInsensitive = true; searchSpec.searchCaseInsensitiveSpecified = true; searchSpec.sortFoldersFirst = true; searchSpec.sortFoldersFirstSpecified = true; searchSpec.searchCaseInsensitiveSpecified = true; searchSpec.details = new FileQueryFlags(); searchSpec.details.fileSize = true; searchSpec.details.fileOwner = false; searchSpec.details.fileType = true; searchSpec.details.fileOwnerSpecified = true; searchSpec.query = new FileQuery[3] { (FileQuery) new FolderFileQuery(), (FileQuery) new VmDiskFileQuery(), new FileQuery() }; VCTask task = new VCTask(this.VcService, this.VcService.Service.SearchDatastoreSubFolders_Task(managedObjects[0], volumeName, searchSpec)); string op = "Search Datastore"; VimClientlContext rstate = ctx; task.WaitForResult(op, rstate); string[] properties1 = new string[1] { "info.result" }; Dictionary <string, object> properties2 = task.GetProperties(properties1); if (properties2.ContainsKey("info.result")) { browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])properties2["info.result"]; } return(browserSearchResultsArray); }
public Dictionary <string, VmdkFileInfo> GetVmdksFileInfo(string fullName, VimClientlContext ctx) { fullName = fullName.Replace("\\", "/"); string str1 = fullName.Substring(0, fullName.LastIndexOf("/")); string fileName = Path.GetFileName(fullName); Dictionary <string, VmdkFileInfo> dictionary = new Dictionary <string, VmdkFileInfo>((IEqualityComparer <string>)StringComparer.InvariantCultureIgnoreCase); try { string str2 = VCService.GetVolumeName(this.Name) + str1; HostDatastoreBrowserSearchResults[] browserSearchResults1 = this.GetBrowserSearchResults(str2, ctx); if (browserSearchResults1 != null) { if (browserSearchResults1.Length != 0) { foreach (HostDatastoreBrowserSearchResults browserSearchResults2 in browserSearchResults1) { string strA = browserSearchResults2.folderPath.TrimEnd('/'); if (browserSearchResults2.file != null && string.Compare(strA, str2, true) == 0) { foreach (VimApi.FileInfo fileInfo in browserSearchResults2.file) { if (!dictionary.ContainsKey(fileInfo.path)) { dictionary.Add(fileInfo.path, new VmdkFileInfo(fileInfo.path, browserSearchResults2.folderPath, (ulong)fileInfo.fileSize)); if (dictionary[fileInfo.path].Name != fileName) { dictionary.Remove(fileInfo.path); } } } break; } } } } } catch (Exception ex) { } return(dictionary); }
private HostDatastoreBrowserSearchResults GetDatastoreSearchResults(string datastorePath, VimClientlContext ctx) { HostDatastoreBrowserSearchResults browserSearchResults = (HostDatastoreBrowserSearchResults)null; ManagedObjectReference[] managedObjects = this.GetManagedObjects(new string[1] { "browser" }); HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); VmDiskFileQuery vmDiskFileQuery = new VmDiskFileQuery(); vmDiskFileQuery.details = new VmDiskFileQueryFlags(); vmDiskFileQuery.details.capacityKb = true; searchSpec.details = new FileQueryFlags(); searchSpec.details.fileSize = true; searchSpec.details.fileOwner = false; searchSpec.details.fileOwner = false; searchSpec.details.fileType = true; searchSpec.details.fileOwnerSpecified = true; searchSpec.query = new FileQuery[2] { (FileQuery) new FolderFileQuery(), (FileQuery)vmDiskFileQuery }; VCTask task = new VCTask(this.VcService, this.VcService.Service.SearchDatastore_Task(managedObjects[0], datastorePath, searchSpec)); string op = "Search Datastore"; VimClientlContext rstate = ctx; task.WaitForResult(op, rstate); string[] properties1 = new string[1] { "info.result" }; Dictionary <string, object> properties2 = task.GetProperties(properties1); if (properties2.ContainsKey("info.result")) { browserSearchResults = (HostDatastoreBrowserSearchResults)properties2["info.result"]; } return(browserSearchResults); }
public Dictionary <string, List <VmdkFileInfo> > GetAllFoldersAndFilesInfo(VimClientlContext ctx) { Dictionary <string, List <VmdkFileInfo> > dictionary = new Dictionary <string, List <VmdkFileInfo> >((IEqualityComparer <string>)StringComparer.InvariantCultureIgnoreCase); string volumeName = VimUtils.GetVolumeName(this.Name); HostDatastoreBrowserSearchResults[] browserSearchResults1 = this.GetBrowserSearchResults(volumeName, ctx); if (browserSearchResults1 != null && browserSearchResults1.Length != 0) { foreach (HostDatastoreBrowserSearchResults browserSearchResults2 in browserSearchResults1) { string str = browserSearchResults2.folderPath.TrimEnd('/'); int length = volumeName.Length; if (length > str.Length) { length = str.Length; } string key = str.Remove(0, length); List <VmdkFileInfo> vmdkFileInfoList = new List <VmdkFileInfo>(); if (browserSearchResults2.file != null) { foreach (VimApi.FileInfo fileInfo in browserSearchResults2.file) { string pattern = "w*-[0-9][0-9][0-9][0-9][0-9][0-9]w*"; if (!Regex.Match(fileInfo.path, pattern, RegexOptions.IgnoreCase).Success) { vmdkFileInfoList.Add(new VmdkFileInfo(fileInfo.path, browserSearchResults2.folderPath, (ulong)fileInfo.fileSize)); } } } if (!dictionary.ContainsKey(key)) { dictionary.Add(key, vmdkFileInfoList); } } } return(dictionary); }
public bool IsFolderOnRootExist(string folderName, VimClientlContext ctx) { VCTask task = new VCTask(this.VcService, this.VcService.Service.SearchDatastoreSubFolders_Task(this.GetManagedObjects(new string[1] { "browser" })[0], VimUtils.GetVolumeName(this.Name), new HostDatastoreBrowserSearchSpec() { matchPattern = new string[1] { folderName }, searchCaseInsensitive = true, searchCaseInsensitiveSpecified = true })); string op = "Browse Datastore"; VimClientlContext rstate = ctx; task.WaitForResult(op, rstate); string[] properties1 = new string[1] { "info.result" }; Dictionary <string, object> properties2 = task.GetProperties(properties1); if (properties2.ContainsKey("info.result")) { HostDatastoreBrowserSearchResults[] browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])properties2["info.result"]; if (browserSearchResultsArray != null && browserSearchResultsArray.Length != 0) { string strB = VimUtils.GetVolumeName(this.Name).Trim(); foreach (HostDatastoreBrowserSearchResults browserSearchResults in browserSearchResultsArray) { if (browserSearchResults.file != null && browserSearchResults.file.Length != 0 && string.Compare(browserSearchResults.folderPath, strB) == 0) { return(true); } } } } return(false); }
public void DownloadFile(string remotePath, string localPath, string fileName, VimClientlContext ctx) { string url = this.VcService.Service.Url; string str1 = url.Substring(0, url.LastIndexOf("sdk") - 1); remotePath = remotePath.Replace("\\", "/"); remotePath = remotePath.Trim("/".ToCharArray()); string fullName = "/" + remotePath + "/" + fileName; if (!this.FileExist(fullName, ctx)) { throw new FileNotFoundException(string.Format("The file {0} not found on datastore {1}.", (object)fullName, (object)this.Name)); } string str2 = Path.Combine(localPath, fileName); string str3 = (str1 + "/folder" + fullName + "?dcPath=" + this.GetDatacenterAndProperties().Name + "&dsName=" + this.Name).Replace("\\ ", "%20"); WebClient webClient = new WebClient(); string str4 = this.VcService.Service.CookieContainer.GetCookies(new Uri(this.VcService.Service.Url))[0].ToString(); webClient.Headers.Add(HttpRequestHeader.Cookie, str4); string address = str3; string fileName1 = str2; webClient.DownloadFile(address, fileName1); }
public Dictionary <string, bool> GetVirtualDisksTypes(string folderName, VimClientlContext ctx) { Dictionary <string, bool> dictionary = new Dictionary <string, bool>((IEqualityComparer <string>)StringComparer.CurrentCultureIgnoreCase); try { string datastorePath = VCService.GetVolumeName(this.Name) + folderName; HostDatastoreBrowserSearchResults[] browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])null; ManagedObjectReference[] managedObjects = this.GetManagedObjects(new string[1] { "browser" }); HostDatastoreBrowserSearchSpec searchSpec = new HostDatastoreBrowserSearchSpec(); searchSpec.matchPattern = new string[1] { "*.vmdk" }; searchSpec.searchCaseInsensitive = true; searchSpec.searchCaseInsensitiveSpecified = true; searchSpec.sortFoldersFirst = true; searchSpec.sortFoldersFirstSpecified = true; searchSpec.searchCaseInsensitiveSpecified = true; searchSpec.details = new FileQueryFlags(); searchSpec.details.fileOwner = false; searchSpec.details.fileOwnerSpecified = true; searchSpec.details.fileSize = true; searchSpec.details.modification = true; searchSpec.details.fileType = true; VmDiskFileQuery vmDiskFileQuery = new VmDiskFileQuery(); vmDiskFileQuery.details = new VmDiskFileQueryFlags(); vmDiskFileQuery.details.thin = true; vmDiskFileQuery.details.thinSpecified = true; FileQuery fileQuery = new FileQuery(); searchSpec.query = new FileQuery[1] { (FileQuery)vmDiskFileQuery }; VCTask task = new VCTask(this._vimService, this._vimService.Service.SearchDatastoreSubFolders_Task(managedObjects[0], datastorePath, searchSpec)); string op = "Browse Datastore"; VimClientlContext rstate = ctx; task.WaitForResult(op, rstate); string[] properties1 = new string[1] { "info.result" }; Dictionary <string, object> properties2 = task.GetProperties(properties1); if (properties2.ContainsKey("info.result")) { browserSearchResultsArray = (HostDatastoreBrowserSearchResults[])properties2["info.result"]; } if (browserSearchResultsArray != null) { if (browserSearchResultsArray.Length != 0) { foreach (HostDatastoreBrowserSearchResults browserSearchResults in browserSearchResultsArray) { List <VmdkFileInfo> vmdkFileInfoList = new List <VmdkFileInfo>(); if (browserSearchResults.file != null) { foreach (VimApi.FileInfo fileInfo in browserSearchResults.file) { if (fileInfo is VmDiskFileInfo && !dictionary.ContainsKey(fileInfo.path)) { dictionary.Add(fileInfo.path, ((VmDiskFileInfo)fileInfo).thin); } } } } } } } catch (Exception ex) { } return(dictionary); }
public void MoveFilesByName(string srcFolder, string srcFile, string tgtFolder, bool force, VimClientlContext ctx) { new VCTask(this.VcService, this.VcService.Service.MoveDatastoreFile_Task(this._vimService.FileManager, this.GetVolumeName(this.Name) + srcFolder + "/" + srcFile, this.GetDatacenterAndProperties().ManagedObject, this.GetVolumeName(this.Name) + tgtFolder + "/" + srcFile, (ManagedObjectReference)null, force, force)).WaitForResult("MoveFile", ctx); }
public void MoveFilesByFullName(string source, string target, string targetFolderName, bool force, VimClientlContext ctx) { if (!this.IsFolderOnRootExist(targetFolderName, ctx)) { this.CreateDirectory(targetFolderName); } new VCTask(this.VcService, this.VcService.Service.MoveDatastoreFile_Task(this._vimService.FileManager, source, this.GetDatacenterAndProperties().ManagedObject, target, (ManagedObjectReference)null, force, force)).WaitForResult("MoveFile", ctx); }