/// <summary> /// Get the file in different search paths corresponding to current culture. /// /// The file name to search is the filename part of path parameter. (Normally path /// parameter should contain only the filename part). /// </summary> /// <param name="file">This is the path to the file. If it has a path, we need to search under that path first.</param> /// <param name="searchPaths">Additional search paths.</param> /// <returns></returns> internal static string LocateFile(string file, Collection <string> searchPaths) { MUIFileSearcher searcher = new MUIFileSearcher(file, searchPaths, SearchMode.First); if (searcher.Result == null || searcher.Result.Count == 0) { return(null); } return(searcher.Result[0]); }
/// <summary> /// Load help file provided. /// </summary> /// <remarks> /// This will load providerHelpInfo from help file into help cache. /// </remarks> /// <param name="providerInfo">ProviderInfo for which to locate help.</param> private void LoadHelpFile(ProviderInfo providerInfo) { if (providerInfo == null) { throw PSTraceSource.NewArgumentNullException(nameof(providerInfo)); } string helpFile = providerInfo.HelpFile; if (string.IsNullOrEmpty(helpFile) || _helpFiles.Contains(helpFile)) { return; } string helpFileToLoad = helpFile; // Get the mshsnapinfo object for this cmdlet. PSSnapInInfo mshSnapInInfo = providerInfo.PSSnapIn; // Search fallback // 1. If PSSnapInInfo exists, then always look in the application base // of the mshsnapin // Otherwise, // Look in the default search path and cmdlet assembly path Collection <string> searchPaths = new Collection <string>(); if (mshSnapInInfo != null) { Diagnostics.Assert(!string.IsNullOrEmpty(mshSnapInInfo.ApplicationBase), "Application Base is null or empty."); // not minishell case.. // we have to search only in the application base for a mshsnapin... // if you create an absolute path for helpfile, then MUIFileSearcher // will look only in that path. helpFileToLoad = Path.Combine(mshSnapInInfo.ApplicationBase, helpFile); } else if ((providerInfo.Module != null) && (!string.IsNullOrEmpty(providerInfo.Module.Path))) { helpFileToLoad = Path.Combine(providerInfo.Module.ModuleBase, helpFile); } else { searchPaths.Add(GetDefaultShellSearchPath()); searchPaths.Add(GetProviderAssemblyPath(providerInfo)); } string location = MUIFileSearcher.LocateFile(helpFileToLoad, searchPaths); if (string.IsNullOrEmpty(location)) { throw new FileNotFoundException(helpFile); } XmlDocument doc = InternalDeserializer.LoadUnsafeXmlDocument( new FileInfo(location), false, /* ignore whitespace, comments, etc. */ null); /* default maxCharactersInDocument */ // Add this file into _helpFiles hashtable to prevent it to be loaded again. _helpFiles[helpFile] = 0; XmlNode helpItemsNode = null; if (doc.HasChildNodes) { for (int i = 0; i < doc.ChildNodes.Count; i++) { XmlNode node = doc.ChildNodes[i]; if (node.NodeType == XmlNodeType.Element && string.Equals(node.Name, "helpItems", StringComparison.OrdinalIgnoreCase)) { helpItemsNode = node; break; } } } if (helpItemsNode == null) { return; } using (this.HelpSystem.Trace(location)) { if (helpItemsNode.HasChildNodes) { for (int i = 0; i < helpItemsNode.ChildNodes.Count; i++) { XmlNode node = helpItemsNode.ChildNodes[i]; if (node.NodeType == XmlNodeType.Element && string.Equals(node.Name, "providerHelp", StringComparison.OrdinalIgnoreCase)) { HelpInfo helpInfo = ProviderHelpInfo.Load(node); if (helpInfo != null) { this.HelpSystem.TraceErrors(helpInfo.Errors); // Add snapin qualified type name for this command.. // this will enable customizations of the help object. helpInfo.FullHelp.TypeNames.Insert(0, string.Format(CultureInfo.InvariantCulture, "ProviderHelpInfo#{0}#{1}", providerInfo.PSSnapInName, helpInfo.Name)); if (!string.IsNullOrEmpty(providerInfo.PSSnapInName)) { helpInfo.FullHelp.Properties.Add(new PSNoteProperty("PSSnapIn", providerInfo.PSSnapIn)); helpInfo.FullHelp.TypeNames.Insert(1, string.Format(CultureInfo.InvariantCulture, "ProviderHelpInfo#{0}", providerInfo.PSSnapInName)); } AddCache(providerInfo.PSSnapInName + "\\" + helpInfo.Name, helpInfo); } } } } } }
/// <summary> /// Search for files in specified search paths. /// </summary> /// <param name="pattern"></param> /// <param name="searchPaths"></param> /// <returns></returns> internal static Collection <string> SearchFiles(string pattern, Collection <string> searchPaths) { MUIFileSearcher searcher = new MUIFileSearcher(pattern, searchPaths); return(searcher.Result); }