none
ReadAllText will not accept string variable RRS feed

  • Question

  • I am using the function ReadAllText to read a text file.  It works fine if I pass it a string like this:

        Dim AllText As String = ""
    
        AllText = My.Computer.FileSystem.ReadAllText("C:\file.txt")
    

    However, I get an unhandled exception when I try to pass it a string variable instead of a literal string like this.

        Dim AllText As String = ""
        Dim strng As String
    
        strng = "C:\file.txt"
    
        AllText = My.Computer.FileSystem.ReadAllText(strng)
    

    Any suggestions?

    Thanks,

    Mike

    Wednesday, July 24, 2019 2:47 PM

Answers

  • I found the problem.  I tried not embedding a null character at the end of the path/filename string I was passing from Fortran to VB.  That fixed the problem.

    Mike

    • Marked as answer by Mike Kipling Wednesday, July 24, 2019 5:57 PM
    Wednesday, July 24, 2019 5:57 PM

All replies

  • Hi

    Works fine for me. Only thing I can suggest is wrap it in a Try ....v Catch with MessageBox.Show(Ex.Message) in the Catch and see exactly what is causing the issue. (in the Try block, use Catch Ex as Exception)


    Regards Les, Livingston, Scotland

    Wednesday, July 24, 2019 3:04 PM
  • It works fine for me too in other programs, but in this one.

    The exception I'm getting also says there are "Illegal characters in path."  I do not see anything wrong with the file path.

    Here is the actual code I'm using.  The commented line works fine.

    Dim AllText As String = "" Dim strng As String = "" strng = Trim(SBG3_GeomOut) AllText = My.Computer.FileSystem.ReadAllText(strng) 'AllText = My.Computer.FileSystem.ReadAllText("C:\Data\SBG3\SBG3_Geometry_New.out")



    Wednesday, July 24, 2019 3:14 PM
  • ReadAllText seems to be working fine using a string variable in place of a quoted string.

    Elsewhere in the same program the same method works fine.  The system is having a problem with the particular file path.  A different file path works fine.

    Thanks,

    Mike

    • Marked as answer by Mike Kipling Wednesday, July 24, 2019 3:37 PM
    • Unmarked as answer by Mike Kipling Wednesday, July 24, 2019 5:57 PM
    Wednesday, July 24, 2019 3:37 PM
  • Hi

    Sound like the path you have in the variable Strng is incorrect. You do not show that (although I think you are saying it should be the same as the commented line).

    Put a break point on the line  strng = Trim(SBG3_GeomOut) and run. When the breakpoint is reached, hover over the SBG3 variable and examine it - is it correct? I will hazard a guess that it is not correct.

    If you can't see anything wrong with it, post it here for completeness.

    *

    Thanks for marking YOUR post as the answer. Sorry I wasted my time.


    Regards Les, Livingston, Scotland






    • Edited by leshay Wednesday, July 24, 2019 3:41 PM
    Wednesday, July 24, 2019 3:37 PM
  • Les,

    I had already tried what you suggested.  There is no problem with the path/filename. 

    The program will not accept a string variable containing this string 

    "C:\Data\SBG3\SBG3_Geometry_New.out"

    but it has no problem with this string

    "C:\Data\SBG3\SBG3_Stress-Life_New.out"

    Both files do exist.

    I'l keep playing with it and eventually figure it out.  Thanks for the help.

    Mike

    Wednesday, July 24, 2019 3:53 PM
  • Hi

    ' I recreated everything and tested
    
    Dim s1 As String = "C:\Data\SBG3\SBG3_Stress-Life_New.out"
    Dim s2 As String = "C:\Data\SBG3\SBG3_Geometry_New.out"
    
    Dim Contents1 As String = My.Computer.FileSystem.ReadAllText(s1)
    Dim Contents2 As String = My.Computer.FileSystem.ReadAllText(s2)
    
    ' both Contents1 and Contents2 were
    ' correctly loaded.


    Regards Les, Livingston, Scotland

    Wednesday, July 24, 2019 4:36 PM
  • Yes, the same thing works fine for me too.  I'm actually passing the string variables back to VB from a Fortran routine with a null embedded following the last character.  I use this method all of the time.

    In this code I'm actually sending two string variables back from the Fortran code.  One variable works fine, but one fails.  I can't see a problem when inspecting the variables in the VB code.

    I'll keep playing with it.


    Wednesday, July 24, 2019 4:53 PM
  • Hi

    When you examined the value of the Strng variable previously, did you check for any spurious blank characters at the end of the string?


    Regards Les, Livingston, Scotland


    • Edited by leshay Wednesday, July 24, 2019 5:01 PM
    Wednesday, July 24, 2019 5:01 PM
  • I found the problem.  I tried not embedding a null character at the end of the path/filename string I was passing from Fortran to VB.  That fixed the problem.

    Mike

    • Marked as answer by Mike Kipling Wednesday, July 24, 2019 5:57 PM
    Wednesday, July 24, 2019 5:57 PM
  • Hi

    Remind me not to reply to anyn more of your questions in the future.


    Regards Les, Livingston, Scotland

    Wednesday, July 24, 2019 6:44 PM