Click here to Skip to main content
15,898,134 members
Articles / Web Development / HTML

My Personal Commander Variant

Rate me:
Please Sign up or sign in to vote.
4.66/5 (13 votes)
5 Oct 2016CPOL14 min read 44.7K   1.2K   47  
A C#/WPF application for displaying folders on a grid and performing combined functions on them.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management.Automation.Runspaces;
using System.Collections.ObjectModel;
using System.IO;
using Lobster.Components.PowerShell;

namespace Lobster.PowerShell
{
    [System.ComponentModel.Composition.Export(typeof(IShellScript))]
    public abstract class ShellScriptBase : IShellScript
    {
        protected Runspace Runspace {get; private set;}
        protected Pipeline Pipeline { get; private set; }

        public Collection<object> Results { get; protected set; }

        public abstract event Action<IShellScript> OperationCompleted;

        public bool IsValid
        {
            get;
            protected set;
        }

        public string ValidityMessage
        {
            get;
            protected set;
        }

        public ShellScriptBase()
        {
            try
            {
                Results = null;

                // create Powershell runspace
                Runspace = RunspaceFactory.CreateRunspace();

                // open it
                Runspace.Open();

                // create a pipeline and feed it the script text
                Pipeline = Runspace.CreatePipeline();

                IsValid = true;
            }
            catch (Exception e)
            {
                ValidityMessage = "Error in ShellScriptBase.Ctor: " + e.ToString();
                IsValid = false;
            }
        }

        public void AddParameter(string name, object value)
        {
            try
            {
                if (!IsValid) return;
                Runspace.SessionStateProxy.SetVariable(name, value);
            }
            catch (Exception e)
            {
                ValidityMessage = "Error in ShellScriptBase.AddParameter: " + e.ToString();
                IsValid = false;
            }
        }

        public void AddInput(object value)
        {
            try
            {
                if (!IsValid) return;
                Pipeline.Input.Write(value);
            }
            catch (Exception e)
            {
                ValidityMessage = "Error in ShellScriptBase.AddInput: " + e.ToString();
                IsValid = false;
            }
        }


        public bool AddScriptFromFile(string pathName)
        {
            try
            {
                if (!IsValid) return false;

                var script = File.ReadAllText(pathName);
                Pipeline.Commands.AddScript(script);
            }
            catch (Exception e)
            {
                ValidityMessage = "Error in ShellScriptBase.AddScriptFromFile: " + e.ToString();
                IsValid = false;
            }
            return IsValid;
        }



        public abstract bool Run();

        public abstract ShellScriptType Type
        {
            get;
        }

        public abstract object Clone();
    }
}

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
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions