|
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Security;
using System.Security.Permissions;
namespace PluginDemo
{
internal class Program
{
// Directory that contains plugins files
private const string pluginDir = "Plugins";
// Plugins files extension mask
private const string pluginExtMask = "*.dll";
static void Main(string[] args)
{
Console.WriteLine("1. Old way of running the plugins - full trust:\r\n");
var pluginMan = new PluginBase.PluginManager();
RunPlugins(pluginMan);
Console.WriteLine(Environment.NewLine);
Console.WriteLine("2. New way of running the plugins - limited trust:\r\n");
var pset = new PermissionSet(PermissionState.None); /* deny all */
/* uncomment the next line to allow file operations for all the plugins */
// pset.AddPermission(new FileIOPermission(FileIOPermissionAccess.Read | FileIOPermissionAccess.Write, Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location)));
/* uncomment the next line to allow working of the WebDownloadPlugin */
// pset.AddPermission(new WebPermission(PermissionState.Unrestricted));
pluginMan = PluginBase.PluginManager.GetInstance(pset);
RunPlugins(pluginMan);
Console.ReadKey();
}
static void RunPlugins(PluginBase.PluginManager pluginMan)
{
var path = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), pluginDir);
foreach (var f in new DirectoryInfo(path).GetFiles(pluginExtMask))
{
var plugin = pluginMan.LoadPlugin(Path.Combine(path, f.Name));
try
{
plugin.Run();
Console.WriteLine("Plugin {0} has finished work\r\n", plugin.Name);
}
catch (Exception ex)
{
Console.WriteLine("An exception occurred in the {0} plugin: {1}\r\n", plugin.Name, ex.Message);
}
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.