123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147 |
- #region --------------- Copyright Dresser Wayne Pignone -------------
- /*
- * $Log: /Wrk/WayneLibraries/Wrk/Common/Assemblies.cs $
- *
- * 4 07-07-06 9:33 Mattias.larsson
- * Fixed some FxCop issues.
- *
- * 3 06-12-22 14:53 roger.månsson
- * Corrected last checkin
- *
- * 2 06-12-22 13:49 roger.månsson
- * Added the GetManifestResourceStreamWithPartialName method
- */
- #endregion
- using System;
- using System.IO;
- using System.Text.RegularExpressions;
- namespace Wayne.Lib
- {
- /// <summary>
- /// Static help class for Assembly issues.
- /// </summary>
- public static class Assemblies
- {
- #region FilePath Methods
- /// <summary>
- /// Get the calling assembly's file path.
- /// </summary>
- /// <returns></returns>
- public static string GetFilePath()
- {
- return GetFilePath(System.Reflection.Assembly.GetCallingAssembly());
- }
- /// <summary>
- /// Get the file path of the given assembly.
- /// </summary>
- /// <param name="assembly"></param>
- /// <returns></returns>
- public static string GetFilePath(System.Reflection.Assembly assembly)
- {
- if (assembly == null)
- throw new NullReferenceException("assembly");
- return Path.GetDirectoryName(assembly.ManifestModule.FullyQualifiedName);
- }
- #endregion
- #region Resource Methods
- /// <summary>
- /// Gets a manifest resource stream from an assembly, where only the file name is known.
- /// </summary>
- /// <param name="knownResourceNameEnd">The known file name, i.e. only the end of the Manifest resource name.</param>
- /// <param name="assemblyWithResource">The assembly that is expected to contain the resource.</param>
- /// <returns>If the resource is found, the stream is returned, otherwise null.</returns>
- public static System.IO.Stream GetManifestResourceStreamWithPartialName(string knownResourceNameEnd,
- System.Reflection.Assembly assemblyWithResource)
- {
- if (knownResourceNameEnd == null)
- throw new ArgumentNullException("knownResourceNameEnd");
- if (assemblyWithResource == null)
- throw new ArgumentNullException("assemblyWithResource");
- string[] manifestResouceNames = assemblyWithResource.GetManifestResourceNames();
- string resourceName = "";
- foreach (string candidate in manifestResouceNames)
- {
- if (candidate.EndsWith(knownResourceNameEnd, StringComparison.InvariantCultureIgnoreCase))
- {
- resourceName = candidate;
- break;
- }
- }
- return assemblyWithResource.GetManifestResourceStream(resourceName);
- }
- /// <summary>
- /// Gets a manifest resource stream from an assembly, using a regular expression.
- /// </summary>
- /// <param name="resourceRegEx">Regular expression for the resource name.</param>
- /// <param name="assemblyWithResource">The assembly that is expected to contain the resource.</param>
- /// <returns>If the resource is found, the stream is returned, otherwise null.</returns>
- public static System.IO.Stream GetManifestResourceStreamWithRegex(string resourceRegEx,
- System.Reflection.Assembly assemblyWithResource)
- {
- if (resourceRegEx == null)
- throw new ArgumentNullException("resourceRegEx");
- if (assemblyWithResource == null)
- throw new ArgumentNullException("assemblyWithResource");
- string[] manifestResouceNames = assemblyWithResource.GetManifestResourceNames();
- string resourceName = "";
- Regex regex = new Regex(resourceRegEx);
- foreach (string candidate in manifestResouceNames)
- {
- if (regex.IsMatch(candidate))
- {
- resourceName = candidate;
- break;
- }
- }
- return assemblyWithResource.GetManifestResourceStream(resourceName);
- }
- ///// <summary>
- ///// NOT IMPLEMENTED YET! Get a resource stream from the executing assembly using the resource name.
- ///// </summary>
- ///// <param name="resourceName">The name of the resource file (in the calling assembly).
- ///// Note that if the file is put under a sub-folder, the resourceName should begin
- ///// with the sub-folder's name followed by a dot (e.g. "MySubFolder.MyResource.txt").</param>
- ///// <returns></returns>
- //public static Stream GetResourceStream(string resourceName)
- //{
- // return GetResourceStream(resourceName, System.Reflection.Assembly.GetCallingAssembly());
- //}
- ///// <summary>
- ///// NOT IMPLEMENTED YET! Get a resource stream from an assembly using the resource name.
- ///// </summary>
- ///// <param name="resourceName">The name of the resource file.
- ///// <note>Note that if the file is put under a sub-folder, the resourceName should begin
- ///// with the sub-folder's name followed by a dot (e.g. "MySubFolder.MyResource.txt").</note></param>
- ///// <param name="assembly"></param>
- ///// <returns></returns>
- //public static Stream GetResourceStream(string resourceName, System.Reflection.Assembly assembly)
- //{
- // throw new NotImplementedException();
- // //if (assembly == null)
- // // throw new NullReferenceException("assembly");
- // //string[] x = assembly.GetManifestResourceNames();
- // //DefaultNamespace
- // ////Microsoft.Win32.
- // //RootNamespace
- // ////assembly.ManifestModule.
- // //return assembly.GetManifestResourceStream(assembly.GetName().Name + "." + resourceName);
- //}
- #endregion
- }
- }
|