C#选择文件对话框

Posted 何以解忧 `唯有暴富

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C#选择文件对话框相关的知识,希望对你有一定的参考价值。

添加System.Windows.Forms引用

选择文件

private void PhotoPathElect_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.OpenFileDialog fileDialog = new System.Windows.Forms.OpenFileDialog();//打开文件对话框 
            fileDialog.InitialDirectory = System.AppDomain.CurrentDomain.BaseDirectory;//初始化路径
            fileDialog.Filter = "图片(*.*)|*.*";//或"图片(*.jpg;*.bmp)|*.jpg;*.bmp";//过滤选项设置,文本文件,所有文件。
            fileDialog.FilterIndex = 0;//当前使用第二个过滤字符串
            fileDialog.RestoreDirectory = true;//对话框关闭时恢复原目录
            fileDialog.Title = "选择图片";
            if (fileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                PhotoPath.Text = fileDialog.FileName;
            }
        }

选择文件或文件夹(只能选择没有目录的文件夹)

 public void SelectFolder()
        {
            System.Windows.Forms.OpenFileDialog folderBrowser = new System.Windows.Forms.OpenFileDialog();
            // Set validate names and check file exists to false otherwise windows will
            // not let you select "Folder Selection."
            folderBrowser.ValidateNames = false;
            folderBrowser.CheckFileExists = false;
            folderBrowser.CheckPathExists = true;
            // Always default to Folder Selection.
            folderBrowser.FileName = "Folder Selection.";
            if (folderBrowser.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string folderPath = Path.GetDirectoryName(folderBrowser.FileName);
                // ...
            }
        }

选择目录

System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
dialog.Description = "please select the folder path";
if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string foldPath = dialog.SelectedPath;
    FolderPath = foldPath;
}

 

以上是关于C#选择文件对话框的主要内容,如果未能解决你的问题,请参考以下文章

C#的文件管理 点击浏览按钮 出现对话框选择文件路径

C++ 代码片段(积累)

日期选择器对话框在片段中不起作用[关闭]

C# WPF:把文件给我拖进来!!!

webView loadUrl 打开浏览器选择对话框

错误时间选择器对话框。这是代码: