Results 1 to 31 of 31

Thread: VB - How to use Alternate Data Stream files

Threaded View

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2002
    Location
    Canada, Toronto
    Posts
    5,803

    VB - How to use Alternate Data Stream files

    Since not many people know about those files, and I see no one posted in the CodeBank about them before, I thought I'd give a small example.

    First of all, Alternate Data Streams (ADS in short), work only on NTFS.
    ADS files are files that "attach" to another file or directory. Those files are completely invisible files; they don't show up in Windows Explorer.

    In this example it's creating the ADS file on the C:\ directory (because it's a code snippet), if you want to do it on your application, you should attach the ADS file to App.Path (or something like that).
    Remember that you can attach an ADS to a file too, just do something like: "C:\myfile.txt:myADS_file.dat" (the myfile.txt file must exist of course).

    Visual Basic supports reading and writing to ADS files, but you can't use functions like Dir or Kill, you have to use API for that.

    I usually use ADS files to save settings which I don't want a regular user to see, like username and password (or something like that). But of course that does not mean that that's the only thing you can save in an ADS file. You can use an ADS file just the same as you use a regular file, except that the file is "invisible"
    VB Code:
    1. Option Explicit
    2.  
    3. Private Const OF_EXIST = &H4000
    4. Private Const OFS_MAXPATHNAME = 128
    5.  
    6. Private Type OFSTRUCT
    7.     cBytes As Byte
    8.     fFixedDisk As Byte
    9.     nErrCode As Integer
    10.     Reserved1 As Integer
    11.     Reserved2 As Integer
    12.     szPathName(OFS_MAXPATHNAME) As Byte
    13. End Type
    14.  
    15. Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
    16. Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
    17.  
    18. Private Type tWindowPos
    19.     Left As Single
    20.     Top As Single
    21.     Width As Single
    22.     Height As Single
    23.     ' put some other settings here that you want to save...
    24. End Type
    25.  
    26. Private Sub cmdDeleteADSFile_Click() ' a button to click if you want to delete the ADS file
    27.     Dim ADSFileName As String
    28.    
    29.     ADSFileName = "C:\:mydata.dat"
    30.    
    31.     If FileExists(ADSFileName) Then ' cannot check with Dir if the ADS file exists
    32.         If DeleteFile(ADSFileName) = 0 Then ' cannot delete an ADS file with Kill
    33.             MsgBox "No able to delete ADS file", vbExclamation
    34.         Else
    35.             MsgBox "ADS file deleted", vbInformation
    36.         End If
    37.     Else
    38.         MsgBox "File does not exist", vbInformation
    39.     End If
    40. End Sub
    41.  
    42. Private Sub Form_Load()
    43.     Dim MyPos As tWindowPos
    44.    
    45.     If FileExists("C:\:mydata.dat") Then ' cannot check with Dir if the ADS file exists
    46.         ' Open the ADS file, and read the data
    47.         Open "C:\:mydata.dat" For Binary Access Read As #1
    48.             Get #1, , MyPos
    49.         Close #1
    50.        
    51.         With Me
    52.             .Left = IIf(MyPos.Left = 0, Me.Left, MyPos.Left)
    53.             .Top = IIf(MyPos.Top = 0, Me.Top, MyPos.Top)
    54.             .Width = IIf(MyPos.Width = 0, Me.Width, MyPos.Width)
    55.             .Height = IIf(MyPos.Height = 0, Me.Height, MyPos.Height)
    56.         End With
    57.     End If
    58. End Sub
    59.  
    60. Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
    61.     Dim MyPos As tWindowPos
    62.    
    63.     If UnloadMode > 1 Then
    64.         GoTo SaveData ' don't prompt for saving, just save without asking if windows is closing
    65.     Else
    66.         If MsgBox("Save window position ?", vbYesNo + vbQuestion, "Save ?") = vbYes Then
    67. SaveData:
    68.             If Me.WindowState <> 0 Then
    69.                 Me.WindowState = 0
    70.                 DoEvents
    71.             End If
    72.            
    73.             With MyPos
    74.                 .Left = Me.Left
    75.                 .Top = Me.Top
    76.                 .Width = Me.Width
    77.                 .Height = Me.Height
    78.             End With
    79.            
    80.             ' Save the data into the ADS file
    81.             Open "C:\:mydata.dat" For Binary Access Write As #1
    82.                 Put #1, , MyPos
    83.             Close #1
    84.         End If
    85.     End If
    86. End Sub
    87.  
    88. Private Function FileExists(ByVal FileName As String) As Boolean
    89.     Dim OF As OFSTRUCT
    90.    
    91.     FileExists = OpenFile(FileName, OF, OF_EXIST) = 1
    92. End Function

    PS
    This is small tool which I found on the internet that alows you to list the ADS files on your comptuer: LADS
    Last edited by CVMichael; Aug 11th, 2005 at 10:08 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width