Click here to Skip to main content
15,880,405 members
Articles / Desktop Programming / Windows Forms

Plug-in Framework

Rate me:
Please Sign up or sign in to vote.
4.84/5 (25 votes)
17 May 2018CPOL4 min read 99.7K   1.9K   132  
Basic framework for building desktop plug-in applications
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using MBG.Extensions.Core;
using Demo.Properties;
using PluginFramework.Utilities;

namespace Demo.Forms
{
    public partial class AvailablePluginsForm : Form
    {
        private IDictionary<string, string> availablePlugins;
        public IDictionary<string, string> SelectedPlugins { get; private set; }

        public AvailablePluginsForm()
        {
            InitializeComponent();
        }
        private void AvailablePluginsForm_Load(object sender, EventArgs e)
        {
            availablePlugins = PluginHelper.FindPlugins();
            clbAvailablePlugins.Items.AddRange(availablePlugins.Keys.ToArray());

            SelectedPlugins = (from p in AppContext.ConfigurationFile.Startup.Plugins
                               select p).ToDictionary(key => key.Title, value => value.AssemblyPath);

            for (int i = 0; i < clbAvailablePlugins.Items.Count; i++)
            {
                if (clbAvailablePlugins.Items[i].ToString().In(SelectedPlugins.Keys))
                {
                    clbAvailablePlugins.SetItemChecked(i, true);
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        private void btnOK_Click(object sender, EventArgs e)
        {
            SelectedPlugins = (from p in availablePlugins
                               where p.Key.In(clbAvailablePlugins.CheckedItems)
                               select p).ToDictionary(key => key.Key, value => value.Value);

            if (SelectedPlugins != null && SelectedPlugins.Count > 0)
            {
                AppContext.ConfigurationFile.Startup.Plugins.Clear();
                foreach (KeyValuePair<string, string> kv in SelectedPlugins)
                {
                    if (!AppContext.ConfigurationFile.Startup.Plugins.Contains(kv.Key))
                    {
                        StartupPlugin plugin = new StartupPlugin();
                        plugin.Title = kv.Key;
                        plugin.AssemblyPath = kv.Value;
                        AppContext.ConfigurationFile.Startup.Plugins.Add(plugin);
                    }
                }
            }

            AppContext.ConfigurationFile.Save(Settings.Default.PluginConfigFile);
        }
    }
}

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Freelancer
Australia Australia
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions