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

发布于 2024-10-19 18:57:19 字数 1342 浏览 1 评论 0原文

我正在摆弄下面的代码。但是,我需要将文件名从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-10-26 18:57:19

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

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

所以:

"C:\\FILEPATH.txt"

或者

@"C:\FILEPATH.txt"

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:

"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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文