views:

137

answers:

1

Does the IO.File.Copy method preserve file attributes? Especially, if I have a write-protected file, will the copy be write-protected to?

+2  A: 

The following code proves that file attributes are copied.

    Dim sourceFile = "z.txt"
    Dim destinationFile = "x.txt"

    Using sw As IO.StreamWriter = IO.File.CreateText(sourceFile)
        sw.Write("testing")
    End Using

    IO.File.SetAttributes(sourceFile, IO.FileAttributes.ReadOnly)
    Debug.WriteLine("Source File ReadOnly = " & (IO.File.GetAttributes(sourceFile) And IO.FileAttributes.ReadOnly))

    IO.File.Copy(sourceFile, destinationFile)
    Debug.WriteLine("Destination File ReadOnly = " & (IO.File.GetAttributes(destinationFile) And IO.FileAttributes.ReadOnly))

And having just used Reflector I see that IO.File.Copy uses kernel32.dll's CopyFile function which has documentation of what is copied and what is not: http://msdn.microsoft.com/en-us/library/aa363851(VS.85).aspx

Tim Murphy