【问题标题】:Selection of a folder path with C#使用 C# 选择文件夹路径
【发布时间】:2013-12-25 09:19:17
【问题描述】:

我有一个 WPF 应用程序,其中有这个方法:

 public static string getFile(List<string> extensions)
 {
     Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
     string ext = "files (", filter = "";
     foreach (string s in extensions)
     {
        ext += s + ",";
        filter += "*." + s + ";";
     }
     ext += ")";
     dlg.Filter =ext+"|"+ filter;
     Nullable<bool> result = dlg.ShowDialog();
     if (result == true)
     {
        return dlg.FileName;
     }
     else return null;
  }

我需要添加另一个简单的方法,它返回一个文件夹路径,我将在其中保存新文件。

  1. 我该怎么做?
  2. 最好的方法是什么?

【问题讨论】:

    标签: c# .net wpf file openfiledialog


    【解决方案1】:

    SaveFileDialog 是您所需要的。来自 MSDN 链接:

    // Configure save file dialog box
    Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
    dlg.FileName = "Document"; // Default file name
    dlg.DefaultExt = ".text"; // Default file extension
    dlg.Filter = "Text documents (.txt)|*.txt"; // Filter files by extension 
    
    // Show save file dialog box
    Nullable<bool> result = dlg.ShowDialog();
    
    // Process save file dialog box results 
    if (result == true)
    {
        // Save document 
        string filename = dlg.FileName;
    }
    

    【讨论】:

      【解决方案2】:

      我建议看看免费的Ookii Dialogs for WPF。我过去曾在商业项目中使用过它,而且效果一直很好。显然对 WPF 的本机支持,但也有很多自定义选项,并在不同版本的 Windows 之间提供更高的一致性。

      【讨论】:

        猜你喜欢
        • 2011-11-11
        • 1970-01-01
        • 2012-06-06
        • 1970-01-01
        • 2022-01-23
        • 1970-01-01
        • 2013-10-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多