我正在将文件上传到目录,我也只是获取文件本身而不是目录和文件夹。
例如,我只使用此代码6122002_Abstract_9-11-07.pdf
获取此hpf.FileName.Substring(hpf.FileName.LastIndexOf("\") + 1)
。
我想要做的是分离6122002,摘要和9-11-07的日期,以便我可以将其插入到sql数据库中。有谁知道怎么做?
答案 0 :(得分:2)
如果你有这个6122002_Abstract_9-11-07.pdf
尝试像
Dim Arr() As String
str = "6122002_Abstract_9-11-07.pdf"
Arr = str.Split("_")
所以数组将包含 6122002 , Abstract 和 9-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)
答案 1 :(得分: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