【问题标题】:Get the last index number VB.net获取最后一个索引号 VB.net
【发布时间】:2017-01-31 00:32:17
【问题描述】:

我是 vb.net 的初学者。如何获取 vb.net 中的最后一个索引号?

例如: 我有 3 个文本文件,它们有不同的文件名。

应用程序每次重新启动时,都会以最后一个索引号继续。

例如: Filename1 之前,重新打开应用程序,然后它会继续 filename2。

我的问题是,每次我重新启动应用程序时,filename1总是打开,它不会继续到filename2。

我该怎么做?谢谢!

【问题讨论】:

标签: vb.net winforms indexing inputstream outputstream


【解决方案1】:

每次重新启动应用程序时,变量都会重新初始化。您可能正在使用一个整数作为索引,并将其初始化为 1。您需要在应用程序关闭时或其他时间将索引的状态存储到持久内存(如文件或数据库)中。然后在启动应用程序时读取文件或数据库以获取最后一个索引号。

【讨论】:

    【解决方案2】:

    您可以通过增加最后一个索引来读取最后一个文件并创建下一个文件。

    Dim newFileIndex as Integer = Directory.GetFiles().Select(
        Function(full)
            Dim nameParts() As String = full.Split("\", StringSplitOptions.RemoveEmptyEntries)
            Dim name As String = nameParts(nameParts.Length - 1) 
            name = name.Substring("filename".Length, name.LastIndexOf(".") - "filename".Length)
            ' At this point you have your file number.
            ' for example, if your file is "filename100.txt" the variable [name] now has 100 in it
            Return Convert.ToInt32(name)
        End Function).Max() + 1
    
       'Now you can create new filename
       filename = "filename" & newFileIndex.ToString() & ".txt"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-09
      • 1970-01-01
      • 2016-02-16
      • 1970-01-01
      • 2011-12-14
      • 1970-01-01
      • 2022-06-14
      • 1970-01-01
      相关资源
      最近更新 更多