使GTK#文件选择器仅选择文件

tec*_*hno 1 c# gtk mono gtk#

我正在使用GTK#FileChooserDialog widget.问题是,即使小部件被命名为文件选择器,您也可以选择带有它的文件夹,它也会返回文件夹名称.有什么办法可以限制它只选择文件吗?我几乎检查了所有找不到的属性.

Dan*_*eih 8

您可以通过在构造函数中定义其Action-Property来限制操作

private void OpenOFD()
{
    Gtk.FileChooserDialog filechooser =
        new Gtk.FileChooserDialog("Choose the file to open",
            this,
            FileChooserAction.Open,
            "Cancel",ResponseType.Cancel,
            "Open",ResponseType.Accept);

    if (filechooser.Run() == (int)ResponseType.Accept) 
    {
        System.IO.FileStream file = System.IO.File.OpenRead(filechooser.Filename);
        file.Close();
    }

    filechooser.Destroy();
}
Run Code Online (Sandbox Code Playgroud)

有4个FolderChooserActions:

  1. CreateFolder:表示创建新文件夹的模式.选择器将让用户命名现有或新文件夹
  2. 打开:仅选择现有文件
  3. 保存:将选择现有文件或键入新文件名
  4. SelectFolder:选择一个existring文件夹