Results 1 to 10 of 10

Thread: Literal String in vb.net [vs2005]

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    Literal String in vb.net [vs2005]

    Hi,

    I am trying to create a file with my streamwriter object.

    My filepath contains spaces ...

    And I need to find a way of how to have this filepath string as a literal string.

    I found out that in C# there is a function where you can add @ before the string ... but have yet to find an equivalant in vb.net.

    What do i do?

    This is the line where it breaks ...

    Dim sw As IO.StreamWriter = New IO.StreamWriter(strFilePath, True)

    strFilePath contains spaces ...

    Im stuck and appreciate any help on the matter.

    Thanks

  2. #2
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: Literal String in vb.net [vs2005]

    So you want to remove the spaces?Do i understand correctly?
    Then just parse the sting and delete every space you find.
    If i understand correctly.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Literal String in vb.net [vs2005]

    First up, there is no reason for an equivalent to C# verbatim string literals in VB. C# is based on C and therefore supports control characters in string literals, e.g. '\t' and '\n'. As such, any character in a C# string literal that is preceded by a backslash is interpreted as a control character. Because file paths contain backslashes you must escape them with another backslash in order that they be interpreted as literal characters, e.g.
    csharp Code:
    1. string path = "C:\\MyFolder\\MyFile.txt";
    Because we know that a file path is not going to contain any control characters we also know that every backslash should be interpreted literally. To this end C# provides the verbatim string literals that you mention. As such, instead of escaping all your backslashes you can use the more readable:
    csharp Code:
    1. string path = @"C:\MyFolder\MyFile.txt";
    Now, VB doesn't support inline control characters so backslashes are always interpreted literally. In VB this is just fine:
    vb.net Code:
    1. Dim path As String = "C:\MyFolder\MyFile.txt"
    So you see, verbatim string literals have nothing whatsoever to do with spaces anyway, so you're barking up the wrong tree. In fact, there is no problem with spaces in file paths anyway. This is absolutely no problem:
    vb.net Code:
    1. Dim path As String = "C:\My Folder\My File.txt"
    2. Dim writer As New StreamWriter(path, True)
    If you're having issues with your code it's nothing to do with the spaces. Perhaps, rather than just saying "it breaks", you should tell us what actually happens. Do you get an error message? If so it would be the sensible thing to do to pass that on to the people you want to diagnose the problem for you.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4
    Raging swede Atheist's Avatar
    Join Date
    Aug 2005
    Location
    Sweden
    Posts
    8,018

    Re: Literal String in vb.net [vs2005]

    Are you getting an error? If so what is the exact error? What does strFilePath contain when you get the error?
    The @ in C# just tells the compiler to ignore all the escape characters (or whatever they're called, ive forgot), for example, to avoid \n from being read as a newline.
    Rate posts that helped you. I do not reply to PM's with coding questions.
    How to Get Your Questions Answered
    Current project: tunaOS
    Me on.. BitBucket, Google Code, Github (pretty empty)

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Literal String in vb.net [vs2005]

    this works
    IO.File.Create(folderName & "\file name with space.txt")
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  6. #6
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,897

    Re: Literal String in vb.net [vs2005]

    and this works

    Code:
                    Dim sw As IO.StreamWriter = _
                    New IO.StreamWriter(folderName & "\foo bar 42.txt", True)
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

  7. #7
    Hyperactive Member mrmojorisin's Avatar
    Join Date
    Oct 2007
    Location
    London Town Vocation: Garden Cricket Genuis
    Posts
    439

    Re: Literal String in vb.net [vs2005]

    Why not stick a lil break point on that line of code and you will see if its this line that is causing all the fuss...Then give us the error your getting??

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Aug 2007
    Posts
    172

    Resolved Re: Literal String in vb.net [vs2005]

    Firstly - Thanks for all the replies.

    What i tried to achieve was just being able to create a file and write to it using the streamwriter object.

    I did for some odd reason get an error message during debugging that indicated that there were invalid characters in my outputpath.

    I automatically assumed that it was something to do with the spaces as I did not use any invalid characters to my knowledge.

    In the meantime, I have rechecked the code and found a small glitch in regard collecting the outputsettings from my isolatedstoragefile which may have caused the error.

    Thanks allot for explaining me more about verbatum string literals etc.

    I can see now that it has absolutely nothing to do with this.

    In anyway, Ive learnt something new by it ... thanks for that.


  9. #9
    Addicted Member
    Join Date
    Mar 2008
    Posts
    143

    Re: Literal String in vb.net [vs2005]

    You could use the following code to determine if you have invalid characters in a file name:

    Code:
    Imports System.IO
    ...
    
    Public Function HasInvalidCharacters(ByVal FileName As System.String) As Boolean
    
     Dim rslt As Boolean = False
    
     If FileName.IndexOfAny(Path.GetInvalidFileNameChars()) >= 0 Then
      rslt = True
     End If
    
     HasInvalidCharacters = rslt
    
    End Function
    Shut up and eat your banana!

  10. #10
    I'm about to be a PowerPoster! kleinma's Avatar
    Join Date
    Nov 2001
    Location
    NJ - USA (Near NYC)
    Posts
    23,373

    Re: Literal String in vb.net [vs2005]

    I really would like to see multiline string literals in VB like C# has... I asked Paul Vick last month if we will see them maybe in the next version of VB, but it did not sound very promising...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width