|
-
Aug 9th, 2005, 10:57 PM
#1
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:
Option Explicit
Private Const OF_EXIST = &H4000
Private Const OFS_MAXPATHNAME = 128
Private Type OFSTRUCT
cBytes As Byte
fFixedDisk As Byte
nErrCode As Integer
Reserved1 As Integer
Reserved2 As Integer
szPathName(OFS_MAXPATHNAME) As Byte
End Type
Private Declare Function OpenFile Lib "kernel32" (ByVal lpFileName As String, lpReOpenBuff As OFSTRUCT, ByVal wStyle As Long) As Long
Private Declare Function DeleteFile Lib "kernel32" Alias "DeleteFileA" (ByVal lpFileName As String) As Long
Private Type tWindowPos
Left As Single
Top As Single
Width As Single
Height As Single
' put some other settings here that you want to save...
End Type
Private Sub cmdDeleteADSFile_Click() ' a button to click if you want to delete the ADS file
Dim ADSFileName As String
ADSFileName = "C:\:mydata.dat"
If FileExists(ADSFileName) Then ' cannot check with Dir if the ADS file exists
If DeleteFile(ADSFileName) = 0 Then ' cannot delete an ADS file with Kill
MsgBox "No able to delete ADS file", vbExclamation
Else
MsgBox "ADS file deleted", vbInformation
End If
Else
MsgBox "File does not exist", vbInformation
End If
End Sub
Private Sub Form_Load()
Dim MyPos As tWindowPos
If FileExists("C:\:mydata.dat") Then ' cannot check with Dir if the ADS file exists
' Open the ADS file, and read the data
Open "C:\:mydata.dat" For Binary Access Read As #1
Get #1, , MyPos
Close #1
With Me
.Left = IIf(MyPos.Left = 0, Me.Left, MyPos.Left)
.Top = IIf(MyPos.Top = 0, Me.Top, MyPos.Top)
.Width = IIf(MyPos.Width = 0, Me.Width, MyPos.Width)
.Height = IIf(MyPos.Height = 0, Me.Height, MyPos.Height)
End With
End If
End Sub
Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
Dim MyPos As tWindowPos
If UnloadMode > 1 Then
GoTo SaveData ' don't prompt for saving, just save without asking if windows is closing
Else
If MsgBox("Save window position ?", vbYesNo + vbQuestion, "Save ?") = vbYes Then
SaveData:
If Me.WindowState <> 0 Then
Me.WindowState = 0
DoEvents
End If
With MyPos
.Left = Me.Left
.Top = Me.Top
.Width = Me.Width
.Height = Me.Height
End With
' Save the data into the ADS file
Open "C:\:mydata.dat" For Binary Access Write As #1
Put #1, , MyPos
Close #1
End If
End If
End Sub
Private Function FileExists(ByVal FileName As String) As Boolean
Dim OF As OFSTRUCT
FileExists = OpenFile(FileName, OF, OF_EXIST) = 1
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|