winform選擇文件夾

using System;
using System.Windows.Forms;

namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 好用一點的打開目錄
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button1_Click(object sender, EventArgs e)
        {
            SaveFileDialog sfd = new SaveFileDialog();
            sfd.DefaultExt = "xls";
            sfd.FileName = "默認名字";
            sfd.Filter = "Excel Document(*.xls)|*.xls";
            //若是設置了默認路徑,那下次打開依然是這個默認路徑,
            //不該該和RestoreDirectory =true同時使用。
            //sfd.InitialDirectory = "C:\\";
            //保存路徑,每次打開,都選擇上次的目錄,
            sfd.RestoreDirectory = true; 
            if (sfd.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return;
            }
        }

        /// <summary>
        /// 難用的打開目錄
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button2_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog fbd = new FolderBrowserDialog();
            //即便設置了這個默認路徑,但滾動條不會自動滾動到那兒
            fbd.SelectedPath = "D:\\app";
            if(fbd.ShowDialog()!=DialogResult.OK)
            {
                return;
            }
        }
    }
}

第一種:app

 

 第二種:spa

相關文章
相關標籤/搜索