Promptu Launcher - Rocket Your Productivity

Home Help Feedback About Download

Source Code Download: CommonFunctions

CommonFunctions is a library of standard functions which ship with Promptu. The source code is provided below to help you in writing your own functions.

You can also download the source code here.

//
//  Copyright 2009 Zach Johnson.  All rights reserved.
//  
//  Use is governed by the Microsoft Public License (Ms-PL)
//  You should have received a copy of the Ms-PL with this program.
//  If not, you can read it at http://www.opensource.org/licenses/ms-pl.html
//

namespace Promptu
{
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Windows.Forms;
    using ZachJohnson.Promptu;
    using ZachJohnson.Promptu.UserModel;

    public class CommonFunctions
    {
        [Documentation("Returns the path of the 'MyDocuments' folder.")]
        public string GetMyDocumentsPath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
        }

        [Documentation("Returns the path of the 'MyMusic' folder.")]
        public string GetMyMusicPath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.MyMusic);
        }

        [Documentation("Returns the path of the 'MyPictures' folder.")]
        public string GetMyPicturesPath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.MyPictures);
        }

        [Documentation("Returns the logical desktop rather than the physical file system location.")]
        public string GetDesktopPath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        }

        [Documentation("Returns the directory used to physically store file objects on the desktop.")]
        public string GetDesktopDirectoryPath()
        {
            return Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
        }

        [Documentation(@"Returns one of two values depending on the current computer's machine name.")]
        [ParameterDocumentation(0, "The machine name to test for.")]
        [ParameterDocumentation(1, "The value to return if the computer's machine name is the provided name.")]
        [ParameterDocumentation(2, "The value to return if the computer's machine name is not the provided name.")] 
        public string SwitchDependingOnMachineName(string machineName, string ifIs, string ifNot)
        {
            if (machineName.ToUpperInvariant() == Environment.MachineName.ToUpperInvariant())
            {
                return ifIs;
            }

            return ifNot;
        }

        [Documentation("Creates the supplied directory path if it does not exist.")]
        [ParameterDocumentation(0, "The path of the directory to create if it does not exist")]
        public string CreateDirectoryIfDoesNotExist(string directory)
        {
            if (!Directory.Exists(directory))
            {
                Directory.CreateDirectory(directory);
            }

            return directory;
        }

        [Documentation("Returns a string with all instances of oldValue replaced with newValue")]
        public string Replace(string text, string oldValue, string newValue)
        {
            return text.Replace(oldValue, newValue);
        }

        [Documentation(@"Formats DateTime.Now according to the specified format string.")]
        [ParameterDocumentation(0, @"A standard date and time format string or a custom date and time format string.
See http://msdn.microsoft.com/en-us/library/az4se3k1(VS.80).aspx for standard date and time format strings and http://msdn.microsoft.com/en-us/library/8kb3ddd4(VS.80).aspx for custom date and time format strings.")]
        public string DateTimeNowToString(string format)
        {
            return DateTime.Now.ToString(format);
        }

        [Documentation("Retrieves a list of all currently running processes.")]
        public ValueList GetCurrentProcesses()
        {
            ValueList valueList = new ValueList();
            foreach (Process process in Process.GetProcesses())
            {
                string name = process.ProcessName;
                if (!valueList.ContainsValue(name))
                {
                    valueList.Add(new ValueListItem(name));
                }
            }

            return valueList;
        }

        [Documentation("Kills all processes with a specified name.")]
        [ParameterDocumentation(0, "The name of the processes to kill.")]
        public string KillAllProcessesNamed(string processName)
        {
            if (processName == null)
            {
                throw new ArgumentNullException("processName");
            }

            Process[] processes = Process.GetProcessesByName(processName);

            int killedCount = 0;

            foreach (Process process in processes)
            {
                if (MessageBox.Show(
                    String.Format("Are you sure you want to kill {0} (handle: {1})?", process.ProcessName, process.Handle),
                    String.Format("Confirm Kill {0}", process.ProcessName),
                    MessageBoxButtons.YesNo,
                    MessageBoxIcon.Information,
                    MessageBoxDefaultButton.Button1) == DialogResult.Yes)
                {
                    process.Kill();
                    killedCount++;
                }
            }

            if (processes.Length == 1 && killedCount == 1)
            {
                return String.Format("Successfully killed {0} process named {2}.", killedCount, processes.Length, processName);
            }
            else
            {
                return String.Format("Successfully killed {0} of {1} processes named {2}.", killedCount, processes.Length, processName);
            }
        }

        [Documentation("On 64 bit machines, returns the full path to the \"Program Files\" (64 bit) "
        + "or the \"Program Files (x86)\" (32 bit) folder depending on the specified architecture.  "
        + "On 32 bit machines, always returns the full path to the \"Program Files\" folder.")]
        [ParameterDocumentation(0, "The version of \"Program Files\" you want returned, i.e '32' or '64'.")]
        public static string GetProgramFilesPath(string version)
        {
            if (version == "32" && IntPtr.Size == 8 || (!String.IsNullOrEmpty(Environment.GetEnvironmentVariable("PROCESSOR_ARCHITEW6432"))))
            {
                return Environment.GetEnvironmentVariable("ProgramFiles(x86)");
            }

            return Environment.GetEnvironmentVariable("ProgramFiles");
        }
    }
}