Click here to Skip to main content
15,887,027 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 263.6K   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 System.IO;
using System.Windows.Forms;

namespace RunVistaGadget21
{
    class FilePathDeterminer
    {
        private String FolderDialogDescription;
        private String FileName;
        private String PreviousFilePath;
        private FileOpenDialogType FODT;

        public FilePathDeterminer(string folderDialogDescription, string defaultFileName, string prevFilePath, FileOpenDialogType fodt)
        {
            FolderDialogDescription = folderDialogDescription;
            FileName = defaultFileName;
            PreviousFilePath = prevFilePath;
            FODT = fodt;
        }

        public string GetFilePath()
        {
            string FilePath = GetSelectedPath();

            if (!String.IsNullOrEmpty(FilePath))
            {
                string PrevFileName = TryGetPreviousFileName();
                if (!String.IsNullOrEmpty(PrevFileName)) 
                {                    
                    FileName = PrevFileName;
                }
                if (FODT == FileOpenDialogType.FolderBrowserDialog)
                {
                    if (Path.GetPathRoot(FilePath) != FilePath)
                    {
                        FilePath += System.IO.Path.DirectorySeparatorChar;
                    }
                    FilePath += FileName;
                }                
            } 

            return FilePath;
        }

        private string TryGetPreviousFileName()
        {
            string PreviousGadgetName = string.Empty;
            if (!String.IsNullOrEmpty(PreviousFilePath))
            {
                PreviousGadgetName = System.IO.Path.GetFileName(PreviousFilePath);
            }

            return PreviousGadgetName;
        }

        private string GetSelectedPath()
        {
            string SelectedPath = String.Empty;
            bool ThisDialogResult = false;
            if (FODT == FileOpenDialogType.FolderBrowserDialog)
            {
                FolderBrowserDialog Dialog = new FolderBrowserDialog();
                Dialog.ShowNewFolderButton = true;
                Dialog.Description = FolderDialogDescription;
                
                ThisDialogResult = (Dialog.ShowDialog() == DialogResult.OK);
                SelectedPath = Dialog.SelectedPath;
            }
            else if (FODT == FileOpenDialogType.OpenFileDialog)
            {
                OpenFileDialog Dialog = new OpenFileDialog();
                Dialog.Title = FolderDialogDescription;
                Dialog.Filter = FileName + "(" + FileName + ")|" + FileName + "|All files (*.*)|*.*";
                
                ThisDialogResult = (Dialog.ShowDialog() == DialogResult.OK);
                SelectedPath = Dialog.FileName;
            }
            return
                    (ThisDialogResult == true) ? SelectedPath : string.Empty;
        }

        public enum FileOpenDialogType
        {
            FolderBrowserDialog,
            OpenFileDialog
        }
    }
}

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