将C:\ Folder \ File更改为C:\\ Folder \\ file

时间:2011-03-02 11:53:14

标签: vb.net text format string-formatting

我正在摆弄以下代码。但是,我需要将文件名从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`

代码很粗糙。我只是测试一些东西。

1 个答案:

答案 0 :(得分:0)

编辑(VB,不是C#,booh:p)

如果你正在写一个像“C:\ FILEPATH.txt”这样的显式字符串,你需要自己加倍\。否则VB会认为\ F是一个特殊字符。用@。

在字符串之前或之前

所以:

"C:\\FILEPATH.txt"

@"C:\FILEPATH.txt"
内部VB只能看到一个\ 因此,如果您从文本框中获取路径,则无需将反斜杠加倍。

修改

好的,所以根据here VB.NET实际上确实没有逃避反斜杠。那么考虑我的答案毫无用处。 (虽然也许我在那里教了一些C-sharper ......?(我知道我知道,我在这里抓住了:p)