我想打开一个依赖于变量" NewFileName"的文件。我试图将它与其位置的路径连接起来。使用VB 2010

时间:2014-10-23 23:34:12

标签: vb.net windows openfiledialog

Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        'Dim theName As String
        'Dim counter As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
        Me.Hide()
        System.Diagnostics.Process.Start("C:\Projects\Test")
        For Each foundfile As String In My.Computer.FileSystem.GetFiles("C:\Projects\Test")
            Dim testFile As System.IO.FileInfo
            testFile = My.Computer.FileSystem.GetFileInfo(foundfile)
            Dim FileName As String = testFile.Name
            Dim FileNameEdited As String = Replace(FileName, ".DWG", "")
            Dim NewFileName As String = FileNameEdited & "_" & TextBox1.Text & ".DWG"
            MsgBox(FileNameEdited)
            Dim check As String = System.IO.Path.GetExtension(foundfile)
            If check = ".DWG" Then
                'My.Computer.FileSystem.RenameFile("C:\Projects\Test" & FileName, NewFileName)
                System.Diagnostics.Process.Start("C:\Projects\Test" & NewFileName)
            End If
        Next
    End Sub

1 个答案:

答案 0 :(得分:0)

好吧,首先,我没有看到你实际重命名文件的地方。我建议在MoveTo上使用FileInfo方法,如下所示:

testFile.MoveTo(NewFileName)

然后,您将新文件名连接到硬编码路径是不正确的。你最后需要反斜杠。让我们假设NewFileName保持值Fleex_main.DWG:您将尝试启动C:\Projects\TestFleex_main.DWG。连接运算符对路径一无所知,只是将两个字符串连在一起。该行应该是:

System.Diagnostics.Process.Start("C:\Projects\Test\" & NewFileName)
'                                                 ^ very important

最后请注意,如果您阅读了StackOverflow的介绍,您会发现强烈建议您提出一个具体问题,并将问题简化为只做出错误行的问题。代码转储使那些想要帮助你的人的生活更加艰难。