使用save filedialog filename重命名目录

时间:2011-07-26 19:15:47

标签: vb.net directory rename

我在这条路径中有一个文件夹 C:\用户\ XXX \桌面\原始\ XXX \ BIN \调试\备份

当我使用“XXX”名称保存我的项目时,我需要使用该保存文件管理名称更改备份文件夹,并且不应覆盖它。

有人可以建议我怎么做:

以下是我如何做的代码,它对我不起作用:

Private Sub SaveProject_Click(ByVal sender As System.Object,ByVal e As System.EventArgs)处理SaveProject.Click

    Using sfdlg As New Windows.Forms.SaveFileDialog
        sfdlg.OverwritePrompt = True
        sfdlg.InitialDirectory = "C:\"
        sfdlg.FileName = "Untitled"
        sfdlg.DefaultExt = "amk"
        sfdlg.Filter = "AquaMark Project|*.amk"
        If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then


            Dim SaveData As New gCanvasData

            With SaveData
                frmDisplay.GCanvas1.UnselectCurrentAnotate()
                .gAnnotates = frmDisplay.GCanvas1.gAnnotates
                .Image = frmDisplay.GCanvas1.Image

            End With

            Using objStreamWriter As New StreamWriter(sfdlg.FileName)
                Dim x As New XmlSerializer(GetType(gCanvasData))
                x.Serialize(objStreamWriter, SaveData)
                objStreamWriter.Close()
            End Using
        End If
        sfdlg.Dispose()
        System.IO.Path.GetFileNameWithoutExtension(sfdlg.FileName)
        IO.Directory.Move(Application.StartupPath + "\Backup\", Application.StartupPath + "\Backup\" & System.IO.Path.GetFileNameWithoutExtension(sfdlg.FileName))
    End Using
End Sub

但任何人都可以清楚地提到我该怎么做吗?

1 个答案:

答案 0 :(得分:0)

根据错误,您很可能无法保存文件并通过相同的SaveFileDialog更改备份文件夹名称。

将其分为两个步骤:

  1. 通过SaveFileDialog保存文件。请务必从SaveFileDialog中捕获文件名,以便在步骤2中使用它,因为在关闭SaveFileDialog窗口后它可能超出了范围。
  2. 使用上面的命令重命名备份文件夹,但 之后,您已关闭SaveFileDialog以释放句柄。
  3. 修改

    正如我在步骤1中所述,您需要将文件名保存在某处,以便在步骤2中使用它。

    Private Sub SaveProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveProject.Click
    
       ' Set up a variable to hold the filenam
       Dim fileName As String
    
        Using sfdlg As New Windows.Forms.SaveFileDialog
            sfdlg.OverwritePrompt = True
            sfdlg.InitialDirectory = "C:\"         
            sfdlg.FileName = "Untitled"         
            sfdlg.DefaultExt = "amk"         
            sfdlg.Filter = "AquaMark Project|*.amk"         
    
            If sfdlg.ShowDialog = Windows.Forms.DialogResult.OK Then
                Dim SaveData As New gCanvasData 
    
                ' Store the filename from the SaveFileDialog
                fileName = sfdlg.FileName             
    
                With SaveData                 
                    frmDisplay.GCanvas1.UnselectCurrentAnotate()                 
                    .gAnnotates = frmDisplay.GCanvas1.gAnnotates                 
                    .Image = frmDisplay.GCanvas1.Image              
                End With              
    
                Using objStreamWriter As New StreamWriter(sfdlg.FileName)                 
                    Dim x As New XmlSerializer(GetType(gCanvasData)) 
                    x.Serialize(objStreamWriter, SaveData)     
                    objStreamWriter.Close()             
                End Using         
            End If         
    
            'Calling Dispose is redundant since sfdlg was in a Using block
            'sfdlg.Dispose()         
            ' You can't use sfdlg.FileName here as the object is out of scope
            'System.IO.Path.GetFileNameWithoutExtension(sfdlg.FileName)
            'IO.Directory.Move(Application.StartupPath + "\Backup\", Application.StartupPath + "\Backup\" & System.IO.Path.GetFileNameWithoutExtension(sfdlg.FileName)) 
             ' Use the value in fileName from above
             System.IO.Directory.Move(Application.StartupPath + "\Backup\", Application.StartupPath + "\Backup\" & System.IO.Path.GetFileNameWithoutExtension(fileName))    
        End Using 
    End Sub