开发者

Rename directory using save filedialog filename

I Have a folder in this path C:\Users\XXX\Desktop\Original\XXX\bin\Debug\Backup

And when I save my project with "XXX" name the same time I need to change the Backup Folder using that save filedialog name and it shouldn't overwrite it.

Can anyone suggest me how to do this:

Here is the code how I am doing it and it didn't work for me:

Private Sub SaveProject_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 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

But can anyone clearly mention me how to do it?


Based on the error, you most likely won't be able to save the file and change the backup folder name through the same SaveFileDialog.

Break it into two steps:

  1. Save the file through the SaveFileDialog. Be sure to capture the filename from SaveFileDialog so you can use it in step 2, as it may be out of scope after you close the SaveFileDialog window.
  2. Rename the backup folder using the command above, but after you've closed the SaveFileDialog so the handle is released.

EDIT

As I noted in step 1, you need to save the filename somewhere so you can use it in step 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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜