winform 實現選擇文件和選擇文件夾對話框


//選擇文件,點擊【瀏覽】,選擇文件
private void button1_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();     //顯示選擇文件對話框
    openFileDialog1.InitialDirectory = "c:\\";
    openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
    openFileDialog1.FilterIndex = 2;
    openFileDialog1.RestoreDirectory = true;

    if (openFileDialog1.ShowDialog() == DialogResult.OK)
    {
        this.textBox1.Text = openFileDialog1.FileName;          //顯示文件路徑
    }
}

 

//選擇文件夾:點擊【瀏覽】,選擇文件夾
private void button4_Click(object sender, EventArgs e)
{
    if (this.folderBrowserDialog1.ShowDialog() == DialogResult.OK)
    {
        if (this.folderBrowserDialog1.SelectedPath.Trim() != "")
            this.textBox4.Text = this.folderBrowserDialog1.SelectedPath.Trim();
    }
}

  選擇文件夾對話框,如果想默認一個文件夾,在click事件一開始添加以下代碼:

 folderBrowserDialog1.SelectedPath =“d:\”;

  呵呵,是不是很簡單呢。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM