wpf winform 選擇檔案或選擇資料夾程式碼

yigu_110發表於2020-11-17
1、選擇單檔案
public string chooseOneFile()
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "視訊(*.MP4;*.avi;*.mov;*.wmv;*.mkv;*.rmvb;*.flv;*.webm)|*.MP4;*.avi;*.mov;*.wmv;*.mkv;*.rmvb;*.flv;*.webm";
    openFileDialog.FilterIndex = 1;
    openFileDialog.RestoreDirectory = true;
    openFileDialog.Title = "選擇檔案";
    openFileDialog.Multiselect = false;//選擇單個
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        return openFileDialog.FileName;
    }
    return null;
}
2、選擇多檔案
 public string[] chooseManyFile()
{
    OpenFileDialog openFileDialog = new OpenFileDialog();
    openFileDialog.Filter = "所有-All(*.*)|*.*|圖片-Images(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG";
    openFileDialog.FilterIndex = 1;
    openFileDialog.RestoreDirectory = true;
    openFileDialog.Title = "選擇檔案";
    openFileDialog.Multiselect = true;//選擇多個
    if (openFileDialog.ShowDialog() == DialogResult.OK)
    {
        return openFileDialog.FileNames;
    }
    return null;
}
3、選擇資料夾
public string chooseFolder()
{
    FolderBrowserDialog m_Dialog = new FolderBrowserDialog();
    m_Dialog.SelectedPath = AppDomain.CurrentDomain.BaseDirectory;
    if (m_Dialog.ShowDialog() == DialogResult.OK)
    {
        return m_Dialog.SelectedPath.Trim();
    }
    return null;
}

相關文章