zoukankan      html  css  js  c++  java
  • WPF选择文件、文件夹和另存为对话框

    一 WPF

    1.1 选择文件对话框

    OpenFileDialog类存在于PresentationFramework.dll程序集。
     1 public string SelectFileWpf()
     2         {
     3             var openFileDialog = new Microsoft.Win32.OpenFileDialog()
     4             {
     5                 Filter = "Text documents (.txt)|*.txt|All files (*.*)|*.*"
     6             };
     7             var result = openFileDialog.ShowDialog();
     8             if (result == true)
     9             {
    10                 return openFileDialog.FileName;
    11             }
    12             else
    13             {
    14                 return null;
    15             }
    16         }

     1.2选择文件夹

    WPF提供了选择文件对话框,但并没有提供选择文件夹的对话框。

    1.3另存为对话框

    SaveFileDialog类位于PresentationFramework.dll 的Microsoft.Win32命名空间

    public static string ChooseSaveFile(string title,string initFolder)
            {
                SaveFileDialog dlg = new SaveFileDialog();
                dlg.Title = title;
                dlg.FileName = "User.txt"; // Default file name
                dlg.DefaultExt = ".txt"; // Default file extension
                dlg.Filter = "Text documents|*.txt"; // Filter files by extension
                dlg.InitialDirectory = initFolder;
    
                // Process save file dialog box results
                if (dlg.ShowDialog() == true)
                {
                    return dlg.FileName;
                }
                else
                {
                    return null;
                }
            }

    二 WinForm

    下面需要添加System.Windows.Forms.dll

    2.1选择文件

    1  public string SelectFile() //弹出一个选择文件的对话框
    2         {
    3             OpenFileDialog file = new OpenFileDialog();
    4             file.ShowDialog();
    5             return file.SafeFileName;
    6         }

    2.2选择文件夹

    using System.Windows.Forms;

    FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { string outDir = folderBrowserDialog.SelectedPath; }

    参考:

    https://docs.microsoft.com/zh-cn/dotnet/api/system.windows.forms.openfiledialog?view=netframework-4.7.2

    C# 文件过滤器filter

  • 相关阅读:
    django-高级
    django-模板
    django-视图
    django笔记一
    redis、mysql、mongodb数据库
    Scrapy-redis分布式+Scrapy-redis实战
    python2 'ascii'编码问题
    【java8新特性】方法引用
    java浮点数运算无法精确的问题
    java中Array和ArrayList区别
  • 原文地址:https://www.cnblogs.com/xinyf/p/10148515.html
Copyright © 2011-2022 走看看