Solved

Help with over writing xml file

Posted on 2014-04-28
6
332 Views
Last Modified: 2014-04-29
Hi,

How do I modify the code below to receive a message a file already exist and over write it?

FolderBrowserDialog1.SelectedPath = Application.StartupPath & "\LinkFiles"
        Dim dlgResult As DialogResult
        dlgResult = FolderBrowserDialog1.ShowDialog()
        If dlgResult = DialogResult.OK Then
            System.IO.File.Copy(Application.StartupPath & "\LinkFiles\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml", _
                FolderBrowserDialog1.SelectedPath & "\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml")
        End If

Thanks,

Victor
0
Comment
Question by:vcharles
  • 3
  • 2
6 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 40027218
FolderBrowserDialog1.SelectedPath = Application.StartupPath & "\LinkFiles"
        Dim dlgResult As DialogResult
        dlgResult = FolderBrowserDialog1.ShowDialog()
        If dlgResult = DialogResult.OK Then
            System.IO.File.Copy(Application.StartupPath & "\LinkFiles\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml", _
                FolderBrowserDialog1.SelectedPath & "\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml")

' something like this
If My.Computer.FileSystem.FileExists(FolderBrowserDialog1.SelectedPath & "\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml")
) Then
    MsgBox(' file exists do you wnat to overwrite yes/no message here dialog)
if dialog = yes
then
File.Copy('Source here, Path.Combine(sFolder,FolderBrowserDialog1.SelectedPath & "\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml"('source here)), True)
Else
    ' do nothing
End IF
END IF
0
 

Author Comment

by:vcharles
ID: 40028487
Hi,

When I try the code below and the file already exist in the folder I want to save it, I receive error message " File already exist" How do I fix this error?

FolderBrowserDialog1.SelectedPath = Application.StartupPath & "\LinkFiles"
        Dim dlgResult As DialogResult
        dlgResult = FolderBrowserDialog1.ShowDialog()
        If dlgResult = DialogResult.OK Then
            System.IO.File.Copy(Application.StartupPath & "\LinkFiles\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml", _
                FolderBrowserDialog1.SelectedPath & "\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml")
        End If

Thanks,

Victor
0
 
LVL 20

Expert Comment

by:ElrondCT
ID: 40028760
Add a ", True" as a third parameter to the File.Copy method. My apologies for failing to put that in the code when I posted it in the prior question.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

Author Comment

by:vcharles
ID: 40029733
Hi,

It worked. How do I notify the users if the file already exist and give them a choice to overwrite or cancel?

Tnaks,

Victor
0
 
LVL 20

Accepted Solution

by:
ElrondCT earned 500 total points
ID: 40029978
        FolderBrowserDialog1.SelectedPath = Application.StartupPath & "\LinkFiles"
        Dim dlgResult As DialogResult
        dlgResult = FolderBrowserDialog1.ShowDialog()
        Dim NewFileName As String = FolderBrowserDialog1.SelectedPath & "\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml"
        If dlgResult = DialogResult.OK Then
            If My.Computer.FileSystem.FileExists(NewFileName) Then
                dlgResult = MessageBox.Show("File " & NewFileName & " already exists. Overwrite?", "File exists", _
                                            MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2)
                If dlgResult = Windows.Forms.DialogResult.No Then
                    Return
                End If
            End If
            System.IO.File.Copy(Application.StartupPath & "\LinkFiles\LinkFinal" & Trim(LoginForm1.username.Text) & ".xml", _
                NewFileName, True)
        End If

Open in new window

If you don't like having the Return in there to bail out of copying the file, coding gets trickier to copy the file either if there's no already existing file or if they've chosen to overwrite. It may be easiest just to have the Copy method twice, though that's normally not considered best programming practice.
0
 

Author Closing Comment

by:vcharles
ID: 40030430
Thank You.
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question