Assemblies.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #region --------------- Copyright Dresser Wayne Pignone -------------
  2. /*
  3. * $Log: /Wrk/WayneLibraries/Wrk/Common/Assemblies.cs $
  4. *
  5. * 4 07-07-06 9:33 Mattias.larsson
  6. * Fixed some FxCop issues.
  7. *
  8. * 3 06-12-22 14:53 roger.månsson
  9. * Corrected last checkin
  10. *
  11. * 2 06-12-22 13:49 roger.månsson
  12. * Added the GetManifestResourceStreamWithPartialName method
  13. */
  14. #endregion
  15. using System;
  16. using System.IO;
  17. using System.Text.RegularExpressions;
  18. namespace Wayne.Lib
  19. {
  20. /// <summary>
  21. /// Static help class for Assembly issues.
  22. /// </summary>
  23. public static class Assemblies
  24. {
  25. #region FilePath Methods
  26. /// <summary>
  27. /// Get the calling assembly's file path.
  28. /// </summary>
  29. /// <returns></returns>
  30. public static string GetFilePath()
  31. {
  32. return GetFilePath(System.Reflection.Assembly.GetCallingAssembly());
  33. }
  34. /// <summary>
  35. /// Get the file path of the given assembly.
  36. /// </summary>
  37. /// <param name="assembly"></param>
  38. /// <returns></returns>
  39. public static string GetFilePath(System.Reflection.Assembly assembly)
  40. {
  41. if (assembly == null)
  42. throw new NullReferenceException("assembly");
  43. return Path.GetDirectoryName(assembly.ManifestModule.FullyQualifiedName);
  44. }
  45. #endregion
  46. #region Resource Methods
  47. /// <summary>
  48. /// Gets a manifest resource stream from an assembly, where only the file name is known.
  49. /// </summary>
  50. /// <param name="knownResourceNameEnd">The known file name, i.e. only the end of the Manifest resource name.</param>
  51. /// <param name="assemblyWithResource">The assembly that is expected to contain the resource.</param>
  52. /// <returns>If the resource is found, the stream is returned, otherwise null.</returns>
  53. public static System.IO.Stream GetManifestResourceStreamWithPartialName(string knownResourceNameEnd,
  54. System.Reflection.Assembly assemblyWithResource)
  55. {
  56. if (knownResourceNameEnd == null)
  57. throw new ArgumentNullException("knownResourceNameEnd");
  58. if (assemblyWithResource == null)
  59. throw new ArgumentNullException("assemblyWithResource");
  60. string[] manifestResouceNames = assemblyWithResource.GetManifestResourceNames();
  61. string resourceName = "";
  62. foreach (string candidate in manifestResouceNames)
  63. {
  64. if (candidate.EndsWith(knownResourceNameEnd, StringComparison.InvariantCultureIgnoreCase))
  65. {
  66. resourceName = candidate;
  67. break;
  68. }
  69. }
  70. return assemblyWithResource.GetManifestResourceStream(resourceName);
  71. }
  72. /// <summary>
  73. /// Gets a manifest resource stream from an assembly, using a regular expression.
  74. /// </summary>
  75. /// <param name="resourceRegEx">Regular expression for the resource name.</param>
  76. /// <param name="assemblyWithResource">The assembly that is expected to contain the resource.</param>
  77. /// <returns>If the resource is found, the stream is returned, otherwise null.</returns>
  78. public static System.IO.Stream GetManifestResourceStreamWithRegex(string resourceRegEx,
  79. System.Reflection.Assembly assemblyWithResource)
  80. {
  81. if (resourceRegEx == null)
  82. throw new ArgumentNullException("resourceRegEx");
  83. if (assemblyWithResource == null)
  84. throw new ArgumentNullException("assemblyWithResource");
  85. string[] manifestResouceNames = assemblyWithResource.GetManifestResourceNames();
  86. string resourceName = "";
  87. Regex regex = new Regex(resourceRegEx);
  88. foreach (string candidate in manifestResouceNames)
  89. {
  90. if (regex.IsMatch(candidate))
  91. {
  92. resourceName = candidate;
  93. break;
  94. }
  95. }
  96. return assemblyWithResource.GetManifestResourceStream(resourceName);
  97. }
  98. ///// <summary>
  99. ///// NOT IMPLEMENTED YET! Get a resource stream from the executing assembly using the resource name.
  100. ///// </summary>
  101. ///// <param name="resourceName">The name of the resource file (in the calling assembly).
  102. ///// Note that if the file is put under a sub-folder, the resourceName should begin
  103. ///// with the sub-folder's name followed by a dot (e.g. "MySubFolder.MyResource.txt").</param>
  104. ///// <returns></returns>
  105. //public static Stream GetResourceStream(string resourceName)
  106. //{
  107. // return GetResourceStream(resourceName, System.Reflection.Assembly.GetCallingAssembly());
  108. //}
  109. ///// <summary>
  110. ///// NOT IMPLEMENTED YET! Get a resource stream from an assembly using the resource name.
  111. ///// </summary>
  112. ///// <param name="resourceName">The name of the resource file.
  113. ///// <note>Note that if the file is put under a sub-folder, the resourceName should begin
  114. ///// with the sub-folder's name followed by a dot (e.g. "MySubFolder.MyResource.txt").</note></param>
  115. ///// <param name="assembly"></param>
  116. ///// <returns></returns>
  117. //public static Stream GetResourceStream(string resourceName, System.Reflection.Assembly assembly)
  118. //{
  119. // throw new NotImplementedException();
  120. // //if (assembly == null)
  121. // // throw new NullReferenceException("assembly");
  122. // //string[] x = assembly.GetManifestResourceNames();
  123. // //DefaultNamespace
  124. // ////Microsoft.Win32.
  125. // //RootNamespace
  126. // ////assembly.ManifestModule.
  127. // //return assembly.GetManifestResourceStream(assembly.GetName().Name + "." + resourceName);
  128. //}
  129. #endregion
  130. }
  131. }