0

我正在将文件上传到一个目录,我也只是获取文件本身而不是目录和文件夹。

例如6122002_Abstract_9-11-07.pdf,通过使用此代码,我只能得到这个hpf.FileName.Substring(hpf.FileName.LastIndexOf("\") + 1)

我想要做的是将 6122002、摘要和 07 年 9 月 11 日的日期分开,这样我就可以将其插入 sql 数据库。有谁知道如何做到这一点?

4

2 回答 2

2

如果你有这个6122002_Abstract_9-11-07.pdf 尝试类似

Dim Arr() As String
str = "6122002_Abstract_9-11-07.pdf"
Arr = str.Split("_")

所以数组将包含6122002Abstract9-11-07.pdf

更新

Dim number As String = Arr(0)
Dim name AS String = Arr(1)
Dim date As String = Arr(2).Substring(0, Arr(2).Length-4)
于 2012-04-12T16:55:18.040 回答
0

要获取没有路径且没有扩展名的文件名,您可以使用

IO.Path.GetFileNameWithoutExtension(f)

而且您可能应该希望将日期部分作为 DateTime。您可以创建一个在其构造函数中采用文件名并将部分解析为属性的类,例如

Module Module1

    Public Class Paper
        Public Property SerialNumber As UInt64
        Public Property Type As String
        Public Property PublishedDate As DateTime

        Public Sub New(filename As String)
            Dim parts() As String = IO.Path.GetFileNameWithoutExtension(filename).Split("_"c)
            ' You should check there are three parts here.
            ' Also, you could use TryParse to make sure the parsing works.
            Me.SerialNumber = UInt64.Parse(parts(0))
            Me.Type = parts(1)
            Dim dateformats() As String = {"dd-MM-yy"} ' could add more if required
            Me.PublishedDate = DateTime.ParseExact(parts(2),
                                                   dateformats,
                                                   New Globalization.CultureInfo("en-GB"),
                                                   Globalization.DateTimeStyles.None)

        End Sub

    End Class

    Sub Main()
        Dim fs = IO.Directory.GetFiles("C:\temp\threeparts")
        Dim papers As New List(Of Paper)

        For Each f In fs
            papers.Add(New Paper(f))
        Next

        For Each p In papers
            Console.WriteLine("Serial: {0}, Type: {1}, Published: {2}", p.SerialNumber, p.Type, p.PublishedDate.ToString("dd-MMM-yyyy"))
        Next

        Console.ReadLine()

    End Sub

End Module
于 2012-04-12T17:54:59.557 回答