【问题标题】:Select Folder Path with savefileDialog使用 savefileDialog 选择文件夹路径
【发布时间】:2011-11-11 21:46:14
【问题描述】:

有没有办法使用对话窗口来获取没有名称文件的文件夹路径?

【问题讨论】:

    标签: c# winforms visual-studio-2010


    【解决方案1】:

    虽然是个老问题,

    我不喜欢那个丑陋的FolderBrowserDialog,所以这是一个对我有用的技巧,它使用SaveFileDialog

    // Prepare a dummy string, thos would appear in the dialog
    string dummyFileName = "Save Here";
    
    SaveFileDialog sf = new SaveFileDialog();
    // Feed the dummy name to the save dialog
    sf.FileName = dummyFileName;
    
    if(sf.ShowDialog() == DialogResult.OK)
    {
        // Now here's our save folder
        string savePath = Path.GetDirectoryName(sf.FileName);
       // Do whatever
    }
    

    【讨论】:

    • 添加对话框。CheckFileExists = false;意味着用户可以在不选择文件的情况下浏览文件夹,虚拟将被接受。
    • SaveFileDialog 默认为 false。
    • 缺点是如果文件名为空用户不能点击确定。 :(
    • 谢谢你。我一直在寻找这个解决方案很长时间。
    • 我还添加了sf.Filter = "Directory | directory"; 以向用户隐藏所有文件。
    【解决方案2】:

    查看FolderBrowserDialog

    // Bring up a dialog to chose a folder path in which to open or save a file.
    private void folderMenuItem_Click(object sender, System.EventArgs e)
    {
        var folderBrowserDialog1 = new FolderBrowserDialog();
    
        // Show the FolderBrowserDialog.
        DialogResult result = folderBrowserDialog1.ShowDialog();
        if( result == DialogResult.OK )
        {
            string folderName = folderBrowserDialog1.SelectedPath;
            ... //Do your work here!
        }
    }
    

    【讨论】:

    • @Luca's:你检查链接了吗?有一个相当广泛的例子。
    • 太长了!在那个例子中告诉如何打开一个 rtf 文件不知道如何选择一个文件夹
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-06-06
    • 1970-01-01
    • 1970-01-01
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 2016-01-28
    相关资源
    最近更新 更多