Copy a file to the application directory


I have this:

if (System.IO.Directory.GetFiles(@"\REPORT\XML", "G*.xml").Length > 0)
    // file exists

And if a file exists how can I copy that file to the application's folder?


Peter Kiers
Peter KiersOperatorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Vel EousResearch & Development ManagerCommented:
SAMIR BHOGAYTATeam LeadCommented:

Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
        ' Specify the directories you want to manipulate.
        Dim path As String = "c:\temp\MyTest.txt"
        Dim path2 As String = path + "temp"

            Dim fs As FileStream = File.Create(path)
            ' Ensure that the target does not exist.

            ' Copy the file.
            File.Copy(path, path2)
            Console.WriteLine("{0} copied to {1}", path, path2)

            ' Try to copy the same file again, which should fail.
            File.Copy(path, path2)
            Console.WriteLine("The second Copy operation succeeded, which was not expected.")

        Catch e As Exception
            Console.WriteLine("The second Copy operation failed, as expected.")
        End Try
    End Sub
End Class
Jacques Bourgeois (James Burger)PresidentCommented:
System.IO.File.Copy ( "YourFileWithPath", System.IO.Path.Combine ( Application.ExecutablePath, "YourFileName" ) );

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Peter KiersOperatorAuthor Commented:
Thanks James, this is the right solution.


Peter Kiers
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.