/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using Everest.Library.Providers.Caching;
namespace Everest.Library.Assemblies{
/// <summary>
///
/// </summary>
public class AssemblyDirectory
{
/// <summary>
/// Gets or sets the cache refresh action.
/// </summary>
/// <value>The cache refresh action.</value>
public ICacheItemRefreshAction CacheRefreshAction
{
get;
set;
}
public AssemblyDirectory(string directory)
{
DirectoryName = directory;
//if (!Directory.Exists(DirectoryPath))
//{
// throw new ArgumentException("The specfied directory must be exist.", directory);
//}
}
/// <summary>
/// Gets or sets the name of the directory.
/// </summary>
/// <value>The name of the directory.</value>
public string DirectoryName { get; private set; }
/// <summary>
/// Gets the directory path.
/// </summary>
/// <value>The directory path.</value>
public string DirectoryPath
{
get
{
return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, DirectoryName);
}
}
/// <summary>
/// Gets the cache key.
/// </summary>
/// <value>The cache key.</value>
private string CacheKey
{
get
{
return string.Format("CacheItem-AssemblyDirectory-{0}", DirectoryPath);
}
}
/// <summary>
/// Gets the assemblies.
/// </summary>
/// <returns></returns>
public IDictionary<string, Assembly> GetAssemblies()
{
if (!DirectoryExists)
{
return null;
}
string[] files = Directory.GetFiles(DirectoryPath, "*.dll");
IDictionary<string, Assembly> assemblies = new Dictionary<string, Assembly>();
for (int i = 0; i < files.Length; i++)
{
using (FileStream fileStream = new FileStream(files[i], FileMode.Open, FileAccess.Read))
{
byte[] bytes = new byte[fileStream.Length];
fileStream.Read(bytes, 0, (int)fileStream.Length);
Assembly assembly = AppDomain.CurrentDomain.Load(bytes);
assemblies.Add(assembly.GetName().Name, assembly);
}
}
return assemblies;
}
/// <summary>
/// Gets a value indicating whether [directory exists].
/// </summary>
/// <value><c>true</c> if [directory exists]; otherwise, <c>false</c>.</value>
public bool DirectoryExists
{
get { return Directory.Exists(DirectoryPath); }
}
/// <summary>
/// Gets the config files.
/// </summary>
/// <returns></returns>
public string[] GetConfigFiles()
{
if (!DirectoryExists)
{
return null;
}
return Directory.GetFiles(DirectoryPath, "*.config");
}
}
}
|