Im using GTK# FileChooserDialog widget.The problem is that even though the widget is named file chooser you can select folders with it and it returns folder names also. Is there any way by which i can restrict it to choosing only files? I have checked almost class all properties i could not find any.

有帮助吗?

解决方案

you can restrict the action by defining its Action-Property in the constructor

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();
}

There are 4 FolderChooserActions:

  1. CreateFolder: Indicates a mode for creating a new folder. The chooser will let the user name an existing or new folder
  2. Open: Will only pick an existing file
  3. Save: Will pick an existing file or type in a new filename
  4. SelectFolder: Pick an existring folder

其他提示

According to the documentation, that behavior depends on the Action property:

  • If it is set to FileChooserAction.Open or FileChooserAction.Save, only files can selected.
  • If it is set to FileChooserAction.SelectFolder or FileChooserAction.CreateFolder, only folders can be selected.
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top