Change C:\Folder\File to C:\\Folder\\file
I am fiddling around with the following code. However, I need the file name restructured from the C:\MY FOLDER\MY FILE
format to the C:\\MY FOLDER\\MY FILE
format. How can I do this?
Public Class Form1
Private Sub TextBox1_Clicked(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Click
'OpenFileDialog1.Title = "Please Select a File"
'OpenFileDialog1.InitialDirectory = "C:temp"
OpenFileDialog1.ShowDialog()
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
Dim strm As System.IO.Stream
strm = OpenFileDialog1.OpenFile()
TextBox1.Text = OpenFileDialog1.FileName.ToString()
If Not (strm Is Nothing) Then
开发者_开发问答 ''insert code to read the file data
strm.Close()
'MessageBox.Show("file closed")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim FILEx As String = "C:\FILEPATH.txt"
If System.IO.File.Exists(FILEx) = True Then
Dim objWriter As New System.IO.StreamWriter(FILEx, False)
objWriter.WriteLine(TextBox1.Text)
objWriter.Close()
End If
End Sub
End Class
`code`
The code is rough.I am just testing some things out.
EDIT (VB, not C#, booh :p)
If you're writing a explicit string like "C:\FILEPATH.txt" you need to double up the \ yourself. Otherwise VB will think \F is a special character. That or preceed the string with a @.
So:
"C:\\FILEPATH.txt"
or
@"C:\FILEPATH.txt"
Internally VB will only see a single \ So if, say, you're getting the path from a textbox you don't need to double the backslashes.
EDIT
Ok, so according to here VB.NET actually does indeed not escape backslashes. Consider my answer useless then. (Although perhaps I educated some C-sharper out there... ? (I know I know, I'm grasping here :p)
精彩评论