IO.File.Copy, File.SetAttributes
Private Sub CopyFile()
Dim TmpFile1, TmpFile2 As String
Dim at1, at2, at3 As FileAttribute
' Create two temporary files
TmpFile1 = IO.Path.GetTempFileName()
TmpFile2 = IO.Path.GetTempFileName()
If System.IO.File.Exists(TmpFile1) Then 'insure they exist
If System.IO.File.Exists(TmpFile2) Then
IO.File.Copy(TmpFile1, TmpFile2, True) ' Copy file
IO.File.SetAttributes(TmpFile1, IO.FileAttributes.Archive)
IO.File.SetAttributes(TmpFile2, IO.FileAttributes.ReadOnly)
at1 = IO.File.GetAttributes(TmpFile1)
at2 = IO.File.GetAttributes(TmpFile2)
' Clean up temporary files
IO.File.Delete(TmpFile1)
' Can't delete TmpFile2 yet why? Its ReadOnly
IO.File.SetAttributes(TmpFile2, IO.FileAttributes.Normal)
at3 = IO.File.GetAttributes(TmpFile2) ' Put it back to Normal
IO.File.Delete(TmpFile2) ' Now can delete
End If
End If
End Sub
IO.Path.GetTempFileName() not only returns a new file name but
actually creates a zero length file. We could not have used
IO.Path.GetRandomFileName because a filename is created but
no file is created. GetAttributes would raise an exception.