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

Full implementation of IShellBrowser

Rate me:
Please Sign up or sign in to vote.
4.86/5 (29 votes)
5 May 2009CPOL4 min read 159.9K   3.2K   113  
A VS-like open and save file dialog implementation.
using System;
using System.IO;
using System.Windows.Forms;

namespace FileDialogs
{
    internal partial class NewFolderDialog : Form
    {
        #region Member Fields

        private static readonly char[] InvalidFileNameChars = Path.GetInvalidFileNameChars();

        private bool m_created = false;
        private string m_path = string.Empty;

        #endregion

        #region Construction

        public NewFolderDialog(string path)
        {
            InitializeComponent();

            m_path = path;
        }

        #endregion

        #region Methods

        private void okButton_Click(object sender, EventArgs e)
        {
            string folderName = nameTextBox.Text.Trim();
            m_path = Path.Combine(m_path, folderName);

            // Check that the folder name isn't empty
            if (string.IsNullOrEmpty(folderName))
            {
                string message = "Type a name for the new folder.";
                if (MessageBox.Show(this, message, Text, MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    DialogResult = DialogResult.Cancel;

                return;
            }

            // Check that the folder name doesn't contain any invalida characters
            if (folderName.IndexOfAny(InvalidFileNameChars) >= 0)
            {
                string message = string.Format("The name '{0}' is not a valid folder name because it contains characters that cannot be used in a folder name. Type another name that does not include the characters <>|*?/.", folderName);
                if (MessageBox.Show(this, message, Text, MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    DialogResult = DialogResult.Cancel;

                return;
            }

            // Check that the folder doesn't already exist
            if (Directory.Exists(m_path))
            {
                string message = string.Format("A folder named '{0}' already exists. Type another name for the folder.", folderName);
                if (MessageBox.Show(this, message, Text, MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    DialogResult = DialogResult.Cancel;

                return;
            }

            try
            {
                m_created = true;
                Directory.CreateDirectory(m_path);
            }
            catch
            {
                m_created = false;
                string message = string.Format("The folder '{0}' could not be created. You may not have access privileges to create a new folder in this location.", folderName);
                if (MessageBox.Show(this, message, Text, MessageBoxButtons.OK) == DialogResult.OK)
                    DialogResult = DialogResult.Cancel;

                return;
            }

            DialogResult = DialogResult.OK;
        }

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets a value indicating weather a folder has been created.
        /// </summary>
        public bool FolderCreated
        {
            get { return m_created; }
            set { m_created = value; }
        }

        /// <summary>
        /// Gets or sets the path of a newly created folder.
        /// </summary>
        public string FolderPath
        {
            get { return m_path; }
            set { m_path = value; }
        }

        #endregion
    }
}

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

Comments and Discussions