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#选择文件对话框的主要内容,如果未能解决你的问题,请参考以下文章