PSYDUCK
PSYDUCK

Reputation: 116

Combine Current Directory with file name in vb.net

I have tested the following two codes .The first one works as I'm giving the full path name but the second one give the error saying the file is not found.The value of the path location is shown same in both the cases.I think i'm not concatenating the stings right.I have also tried to give the filename without the extension.Any help?

1st code

    Dim content As String
    Dim path As String
    path = "C:\Users\****\bin\Debug\tns.txt"
    MessageBox.Show(path)
    Try
        Dim sr As New StreamReader(path)
        content = sr.ReadToEnd()
        Console.WriteLine(content)
        MessageBox.Show(content)
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

2nd code

 Dim content As String
    Dim path As String
    path = Directory.GetCurrentDirectory() + "\tns.txt"
    MessageBox.Show(path)
    Try
        Dim sr As New StreamReader(path)
        content = sr.ReadToEnd()
        Console.WriteLine(content)
        MessageBox.Show(content)
    Catch ex As Exception
        MessageBox.Show(ex.Message.ToString())
    End Try

Upvotes: 0

Views: 5986

Answers (2)

M. Adeel Khalid
M. Adeel Khalid

Reputation: 1796

Works with single slash "\tnx.txt"

Upvotes: 0

apc
apc

Reputation: 5566

path = IO.Path.Combine(Directory.GetCurrentDirectory(), "tns.txt")

Upvotes: 2

Related Questions