|
-
May 15th, 2008, 07:53 AM
#1
Thread Starter
Addicted Member
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
-
May 15th, 2008, 08:20 AM
#2
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.
-
May 15th, 2008, 08:24 AM
#3
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:
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:
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:
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:
Dim path As String = "C:\My Folder\My File.txt" 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.
-
May 15th, 2008, 08:25 AM
#4
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.
-
May 15th, 2008, 08:29 AM
#5
Re: Literal String in vb.net [vs2005]
this works
IO.File.Create(folderName & "\file name with space.txt")
-
May 15th, 2008, 08:32 AM
#6
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)
-
May 15th, 2008, 08:44 AM
#7
Hyperactive Member
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??
-
May 15th, 2008, 11:41 AM
#8
Thread Starter
Addicted Member
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.
-
May 15th, 2008, 03:54 PM
#9
Addicted Member
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!
-
May 15th, 2008, 03:57 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|