将 C:\Folder\File 更改为 C:\\Folder\\file
我正在摆弄下面的代码。但是,我需要将文件名从 C:\MY FOLDER\MY FILE
格式重组为 C:\\MY FOLDER\\MY FILE
格式。我该怎么做?
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`
代码很粗糙。我只是测试一些东西。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
编辑(VB,而不是C#,嘘:p)
如果您正在编写像“C:\FILEPATH.txt”这样的显式字符串,您需要自己将\加倍。否则VB会认为\F是一个特殊字符。或者在字符串前面加上@。
所以:
或者
VB内部只会看到一个\
因此,如果您从文本框中获取路径,则无需使用双反斜杠。
编辑
好的,所以根据这里VB.NET实际上确实做到了不转义反斜杠。那我的回答就认为没用了。 (虽然也许我在那里教过一些 C 语言精尖的人……?(我知道我知道,我在这里抓住了:p)
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:
or
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)