Click here to Skip to main content
15,891,423 members
Articles / Programming Languages / C#

Create a Vista Gadget Using Visual Studio IDE (updated)

Rate me:
Please Sign up or sign in to vote.
4.84/5 (46 votes)
8 Feb 2011CPOL13 min read 264.5K   19.5K   254  
This article describes how to use Visual Studio for developing a Vista Gadget.
using System;
using System.Collections.Generic;
using System.Text;
using EnvDTE;
using EnvDTE80;
using System.IO;

namespace RunVistaGadget25.AdditionalApplicationsWrappers
{
    public class ApplicationRunner
    {
        private bool _IsRunAsWinApp;
        private String _ApplicationArgs = String.Empty;
        public String GadgetPath;
        protected String CalledApplicationPath = String.Empty;
        protected EnvDTE.Project project;
        static GadgetProperties gp;


        public ApplicationRunner(DTE2 applicationObject)
            : this(applicationObject, false)
        {

        }

        public ApplicationRunner(DTE2 applicationObject, bool isRunAsWinApp)
        {
            if (applicationObject.Solution.Projects.Count > 0)
            {
                project = applicationObject.Solution.Projects.Item(1);
            }
            _IsRunAsWinApp = isRunAsWinApp;
            GadgetPath = "\"" + GadgetProp.GadgetOutputPath + "\"";
        }

        public static GadgetProperties GadgetProp
        {
            get
            {
                if (gp == null)
                    gp = new GadgetProperties();
                return gp;
            }
        }

        public virtual String GetAppArguments()
        {
            return String.Empty;
        }

        public virtual String GetOutputAndRunApplication()
        {
            this._ApplicationArgs = GetAppArguments();
            return GetCommonOutputAndRunApplication();
        }

        private String GetCommonOutputAndRunApplication()
        {
            System.Diagnostics.Process AppProcess = new System.Diagnostics.Process();
            if (_IsRunAsWinApp == false)
            {
                AppProcess.StartInfo.UseShellExecute = false;
                AppProcess.StartInfo.RedirectStandardOutput = true;
                AppProcess.StartInfo.CreateNoWindow = true;
                AppProcess.StartInfo.WorkingDirectory = Path.GetDirectoryName(CalledApplicationPath);
            }
            AppProcess.StartInfo.Arguments = _ApplicationArgs;

            AppProcess.StartInfo.FileName = CalledApplicationPath;

            AppProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal;

            String ApplicationOutput = String.Empty;
            try
            {
                bool result = AppProcess.Start();

                if (_IsRunAsWinApp == false)
                {
                    AppProcess.WaitForExit();
                    ApplicationOutput = AppProcess.StandardOutput.ReadToEnd();
                }
                else
                {
                    if (result == true)
                    {
                        ApplicationOutput = "Application has been run sucessfully.";
                    }
                    else
                    {
                        ApplicationOutput = "Could not start application.";
                    }
                }
                AppProcess.Close();
            }
            catch (Exception ex)
            {
                ApplicationOutput = String.Format("Exception has occured. Exception type:{0} \nException message: {1} {1} \nException stack trace: \n{2} ", ex.Source, ex.Message, ex.StackTrace);
            }
            return ApplicationOutput;
        }

        public bool IsExistCalledApplication(string applicationPath, string applicationName)
        {
            bool IsExistApplication = false;
            string TestAppExistanceRegexExpression = @"\\" + applicationName;
            System.Text.RegularExpressions.Regex TestAppExistance = new System.Text.RegularExpressions.Regex(
                TestAppExistanceRegexExpression, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
            if (TestAppExistance.IsMatch(applicationPath))
            {
                IsExistApplication = true;
            }

            return IsExistApplication;
        }

    }
}

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

Comments and Discussions