#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
{
///
/// Static help class for Assembly issues.
///
public static class Assemblies
{
#region FilePath Methods
///
/// Get the calling assembly's file path.
///
///
public static string GetFilePath()
{
return GetFilePath(System.Reflection.Assembly.GetCallingAssembly());
}
///
/// Get the file path of the given assembly.
///
///
///
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
///
/// Gets a manifest resource stream from an assembly, where only the file name is known.
///
/// The known file name, i.e. only the end of the Manifest resource name.
/// The assembly that is expected to contain the resource.
/// If the resource is found, the stream is returned, otherwise null.
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);
}
///
/// Gets a manifest resource stream from an assembly, using a regular expression.
///
/// Regular expression for the resource name.
/// The assembly that is expected to contain the resource.
/// If the resource is found, the stream is returned, otherwise null.
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);
}
/////
///// NOT IMPLEMENTED YET! Get a resource stream from the executing assembly using the resource name.
/////
///// 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").
/////
//public static Stream GetResourceStream(string resourceName)
//{
// return GetResourceStream(resourceName, System.Reflection.Assembly.GetCallingAssembly());
//}
/////
///// NOT IMPLEMENTED YET! Get a resource stream from an assembly using the resource name.
/////
///// The name of the resource file.
///// 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").
/////
/////
//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
}
}