2016-06-22 7 views
0

を失敗した私は、文字列型の2つの変数を連結しようとすると、私の結果は、最初の変数です:VB - 2つの文字列、変数の連結は

Function newXlsx(ByVal sFilepath As String) As Boolean 
    Dim sFileName As String 
    Dim sTest As String 

    sTest = sFilepath.Trim() 
    sFileName = Format(Now, "yyyy-MM-dd") & ".xls*" 
    MsgBox(sTest & "\" & sFileName) 

    If My.Computer.FileSystem.FileExists(sFilepath & "\" & sFileName) Then 
     MsgBox("File found.") 
    Else 
     MsgBox("File not found.") 
    End If 
End Function 

注:sFilepath関数に与えられたが、「Hです:\ A74 "の後ろに複数のスペースがあるので、" 012 "、" 0 "、なぜか私はTrim()という文字列です。だからsTestは "H:\ A74"で、sFileNameは "2016-06-22.xls *"ですが、結果は "H:\ A74 \ 2016-06-22.xls *"ではなく、ちょうど "H:\ A74"です。あなたの代わりにあなたのトリムされた文字列のMy.Computer.FileSystem.FileExists(sFilepath & "\" & sFileName)を使用している

Function newXlsx(ByVal sFilepath As String) As Boolean 
    Dim sFileName = Date.Now.ToString("yyyy-MM-dd") & ".xls" 
    Dim path = System.IO.Path.Combine(sFilepath.Trim(), sFileName)  
    Dim exists = System.IO.File.Exists(path) 
    If exists Then 
     MsgBox("File found.") 
    Else 
     MsgBox("File not found.") 
    End If 
    Return exists 
End Function 

注:このような問題を防ぐために

+0

IO.Pathメソッドを使用しますブレークポイントとコードをステップ実行すると、これらの問題を見つけるのに役立ちます。 –

+0

私は可能な限り正確な問題を記述しました。私は、その問題を見つけるためにbpを使用しました。 – flohdieter

+0

拡張子の末尾にアスタリスクが付いていると問題になります。 –

答えて

4

使用System.IO.Path.Combine


更新:たぶん、あなたのパスに無効な文字が含まれています。

ReadOnly InvalidPathChars As String = New String(Path.GetInvalidPathChars()) 

Public Function RemoveInvalidPathChars(dirOrFileName As String) As String 
    dirOrFileName = dirOrFileName.Trim() 
    For Each c As Char In InvalidPathChars 
     dirOrFileName = dirOrFileName.Replace(c, "") 
    Next 
    Return dirOrFileName 
End Function 

を次にそれを使用するに応じて、あなたの方法を変更します:あなたはそれらを削除する方法を使用することができ

Function newXlsx(ByVal sFilepath As String) As Boolean 
    Dim sFileName = Date.Now.ToString("yyyy-MM-dd") & ".xls" 
    Dim safePath = RemoveInvalidPathChars(sFilepath) 
    Dim path = System.IO.Path.Combine(safePath, sFileName) 
    Dim exists = System.IO.File.Exists(path) 
    If exists Then 
     MsgBox("File found.") 
    Else 
     MsgBox("File not found.") 
    End If 
    Return exists 
End Function 
+0

コードをコピーしましたが、System.ArgumentExceptionが発生しました。「不正な文字」と表示されます – flohdieter

+0

@flohdieter:この例外はどこにありますか? –

+0

'Dim path = System.IO.Path.Combine(sFilepath.Trim()、sFileName)'行 – flohdieter

0

あなたが使用する方法を学ぶ必要があります

Function newXlsx(ByVal sFilepath As String) As Boolean 
    Dim spath As String 
    spath = IO.Path.Combine(sFilepath.Trim, DateTime.Now.ToString("yyyy-MM-dd")) 
    spath = IO.Path.ChangeExtension(spath, "xls") 

    MsgBox(spath) 

    If My.Computer.FileSystem.FileExists(spath) Then 
     MsgBox("File found.") 
    Else 
     MsgBox("File not found.") 
    End If 
End Function 
+0

'spath = IO.Path.Combine(sFilepath.Trim、DateTime.Now.ToString(" yyyy-MM-dd "))'行はシステムをスローします。ArgumentException、それは "不正な文字"と言います – flohdieter

+0

これは、sFilepathに違法文字があり、IO.Pathの別の利点があることを意味します。 IO.Pathには他の方法があります。https://msdn.microsoft.com/en-us/library/system.io.path%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396 – dbasnett

+0

私はこのnewXlsx( "H:\ A74_Handscan_Data") を使って試してみました。 _Dataの後には空白がたくさんありますが、表示されません。 – dbasnett

関連する問題