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.

































































































600

1200

1800

2400

3000

3600

4200

4800

5400

6000

6600

7200

7800

8400

9000

9600

10200

10800

11400

12000

12600

13200

13800

14400

15000

15600

16200

16800

17400

18000

18600

19200

19800

20400

10/22/2015
TheLonelyProgrammer.Net
Alone in a sea of non-programmers
Home File Copy Path List Drives List Files Special Folders Accessing and changing file times