【问题标题】:Getting the first 6 strings in a string array in VB在VB中获取字符串数组中的前6个字符串
【发布时间】:2023-03-10 09:59:01
【问题描述】:

我有一个字符串数组,它从一个目录中获取所有文件。 Dim files() as string = IO.Directory.GetFiles(xxx) 这些文件被添加为 TreeView 上的节点。我遇到的问题是,当数组中有 300 个文件时。我遍历并获取每个文件并将所有 300 个文件添加到 Treeview。但我只想从数组中获取前 100 个文件,然后只添加这些文件。我觉得这应该很简单,但试图弄清楚如何从数组中获取前 100 个字符串(文件)却让我心烦意乱。任何帮助将不胜感激。

【问题讨论】:

  • Dim firstFiles() as string = IO.Directory.GetFiles(...).Take(6) 或 100 或 13 任你喜欢
  • 数一数...或使用Take

标签: arrays string vb.net treeview


【解决方案1】:

选项 1) 使用 take()

Dim files() as string = IO.Directory.GetFiles("C:\Temp").Take(6)

选项 2) 使用 for 循环

Dim files() as string = IO.Directory.GetFiles("C:\Temp")
For i as Integer = 0 to 5
    TreeView1.Nodes.Add(files(i))
Next

【讨论】:

  • 欢迎您!如果它解决了您的需求,请标记为答案。
  • 如果您使用 For 循环,请添加额外的检查以确保“i”小于返回的文件数!
  • opt 1 最后不需要ToArray() 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-27
  • 2021-05-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多