VB.NET连接字符串会增加不需要的空间

时间:2017-11-08 11:46:57

标签: vb.net

我完全不知道为什么会这样。我正在编写一个小应用程序,为带有日期属性的图像文件添加前缀。但是,当我附加.jpg扩展名时,它会在文件名和扩展名之间添加一个空格。例如" 2017-11-07_13:04:01 .jpg"。

 Private Function ProcessFile(ByVal path As String) As String
    Const DATE_TAKEN As Integer = &H9003
    Dim img As Image = Image.FromFile(path)
    Dim dateTaken As String
    Dim pitem As PropertyItem
    If img.PropertyIdList.Contains(DATE_TAKEN) Then

        pitem = img.GetPropertyItem(DATE_TAKEN)
        dateTaken = Encoding.UTF8.GetString(pitem.Value, 0, pitem.Value.Length).ToString

        dateTaken = Trim(Replace(Replace(dateTaken, ":", "-"), " ", "_"))
        dateTaken = Replace(dateTaken & ".jpg", " ", "")
    Else
        dateTaken = "Not available"
    End If
    img.Dispose()
    pitem = Nothing
    Return dateTaken
End Function

正如你所看到的,我试图通过修剪和更换来替换空间,但没有任何作用。

1 个答案:

答案 0 :(得分:1)

当您从文件中获取日期时,它包含一个“VbNullChar”

您需要做的就是添加一行代码来修剪它..

Private Function ProcessFile(ByVal path As String) As String
    Const DATE_TAKEN As Integer = &H9003
    Dim img As Image = Image.FromFile(path)
    Dim dateTaken As String
    Dim pitem As PropertyItem
    If img.PropertyIdList.Contains(DATE_TAKEN) Then
        pitem = img.GetPropertyItem(DATE_TAKEN)
        dateTaken = Encoding.UTF8.GetString(pitem.Value, 0, pitem.Value.Length).ToString
        dateTaken = dateTaken.Trim(vbNullChar)
        dateTaken = Trim(Replace(Replace(dateTaken, ":", "-"), " ", "_"))
        dateTaken = Replace(dateTaken & ".jpg", " ", "")
    Else
        dateTaken = "Not available"
    End If
    img.Dispose()
    pitem = Nothing
    Return dateTaken
End Function