【问题标题】:How to add files to a list in vala?如何将文件添加到vala的列表中?
【发布时间】:2021-05-08 19:12:14
【问题描述】:

我想将文件添加到列表中,然后在 for 循环中访问它们。我就是这样做的:

private get_app_list () {
        var file = new File.new_for_path (/usr/share/applications);
        List<File> app_list = new List<File> ();
        
        foreach (File desktop_file in app_list) {
            // other code here
        }
    }

访问存储在目录中的文件然后将它们添加到列表中的正确方法是什么??

【问题讨论】:

    标签: vala gio


    【解决方案1】:
    using Posix;
    ...
    List<File> app_list = new List<File> ();
    //Open directory. Returns null on error
    var dirHandle = Posix.opendir("/usr/share/applications");
    unowned DirEnt entry;
    //While there is an entry to read in the directory
    while((entry = readdir(dir)) != null) {
        //Get the name
        var name = (string) entry.d_name;
        //And add a new file to the app_list
        app_list.add(new File.new_for_path("/usr/share/applications"+name);
    }
    

    【讨论】:

      【解决方案2】:

      如果您只想在系统上显示可用的应用程序,您可以使用 Gio-2.0 库提供的实用程序。将dependency ('gio-2.0'), 添加到您的meson.build 文件后,您可以使用类似于以下的代码:

      /* We use a `GListStore` here, which is a simple array-like list implementation
      * for manual management.
      * List models need to know what type of data they provide, so we need to
      * provide the type here. As we want to do a list of applications, `GAppInfo`
      * is the object we provide.
      */
      var app_list = new GLib.ListStore (typeof (GLib.AppInfo));
      var apps = GLib.AppInfo.get_all ();
      foreach (var app in apps) {
          app_list.append (app);
      }
      

      但是,如果您需要列出目录中的文件,也可以使用同一 gio-2.0 库提供的更高级别的 API。这是枚举"/usr/share/applications/"中的文件的示例代码

      void main () {
          var app_dir = GLib.File.new_for_path ("/usr/share/applications");
          try {
              var cancellable = new Cancellable ();
      
              GLib.FileEnumerator enumerator = app_dir.enumerate_children (
                  GLib.FileAttribute.STANDARD_DISPLAY_NAME,
                  GLib.FileQueryInfoFlags.NOFOLLOW_SYMLINKS,
                  cancellable
              );
      
              FileInfo ? file_info = null;
              while (!cancellable.is_cancelled () && 
                     ((file_info = enumerator.next_file (cancellable)) != null)) {
                  // Ignore directories
                  if (file_info.get_file_type () == GLib.FileType.DIRECTORY) {
                      continue;
                  }
                  // files could be added to a list_store here.
                  /*
                   * var files_list = new GLib.ListStore (typeof (GLib.FileInfo));
                   * files_list.append (file_info);
                   */
                  print (file_info.get_display_name () + "\n");
              }
          } catch (GLib.Error err) {
              info ("%s\n", err.message);
          }
      }
      

      我希望这能有所帮助。

      【讨论】:

        猜你喜欢
        • 2020-01-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-13
        • 1970-01-01
        • 2018-07-26
        • 1970-01-01
        相关资源
        最近更新 更多