Link to home
Start Free TrialLog in
Avatar of Peter Kiers
Peter KiersFlag for Netherlands

asked on

Copy a file to the application directory

Hi,

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?

Greetings,


Peter Kiers
Avatar of Vel Eous
Vel Eous

Avatar of SAMIR BHOGAYTA
Hello,

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"

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

            ' 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
ASKER CERTIFIED SOLUTION
Avatar of Jacques Bourgeois (James Burger)
Jacques Bourgeois (James Burger)
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Peter Kiers

ASKER

Thanks James, this is the right solution.

Greetings,

Peter Kiers