VirtualVolume

From Xojo Documentation

Class (inherits from Object)

Enables you to create and maintain a hierarchy of "virtual" files within one physical file. Basic reading and writing text and binary streams are supported.

Properties
Root fa-lock-32.png


Methods
Flush

Notes

The FolderItem class has two methods and one property for working with virtual volumes. Use the CreateAsVirtualVolume and OpenAsVirtualVolume methods to create and open virtual volumes. The VirtualVolume property of the FolderItem class returns the VirtualVolume if the FolderItem is in a virtual volume.

Once you've created a VirtualVolume, you can get its Root and navigate to it using the same FolderItem methods that you would for real files. Use the Child method of the FolderItem class to save and read files in the virtual volume.

Virtual files are cross-platform compatible, and support basic reading and writing as text or binary files. The SaveAsPicture method of the FolderItem class does not work for virtual volumes. Virtual volumes do not support resource forks.

Virtual volumes flush their data to disk whenever a virtual volume is closed. This helps ensure that your data is safely on disk in the event of a crash or loss of power.

Filenames can be up to 223 bytes long. Paths are not supported, but directories are (e.g., create a virtual directory with the CreateAsFolder method of the FolderItem class, and navigate to it with the Child or Item methods of this class). The virtual file system supports four-byte type codes (accessed via f.MacType), but does not support creator codes.

Examples

The following example creates a VirtualVolume, gets its Root property, and writes a text file to the virtual volume.

Var vv As VirtualVolume
Var realFile As FolderItem
Var virtFile As FolderItem
Var outp As TextOutputStream
realFile = New FolderItem("VV")
vv = realFile.CreateVirtualVolume
If vv = Nil Then
MessageBox("Unable to create virtual volume")
Else
virtFile = vv.Root.Child("Virtual File.txt")
outp = TextOutputStream.Create(virtFile)
outp.Write("Hello world!")
End If

To access an existing virtual volume, use the OpenAsVirtualVolume method instead of the CreateVirtualVolume method of the FolderItem class.

The following example saves the contents of a TextArea as a file in a VirtualVolume. It is passed the name of the file.

Sub SaveTextFile(s As String)
//creates a VirtualVolume and saves the text in the TextArea as a file

Var f As FolderItem
Var b As BinaryStream
Var v As New VirtualVolume
f = FolderItem.ShowSaveFileDialog("", "myVirtualVolume")
If f <> Nil Then
v = f.CreateVirtualVolume
f = v.Root.Child(s)
b = BinaryStream.Create(f, False)
b.Write(TextArea1.Value)
b.Close
End If
End Sub

The following example reads the text file that was saved by the previous example. It is passed the name of the file.

Sub ReadTextFile(s As String)
// reads the text file in the VirtualVolume
Var f, virtualFile As Folderitem
Var v As VirtualVolume
f = FolderItem.ShowOpenFileDialog("myVirtualVolume")
If f <> Nil Then
v = f.OpenAsVirtualVolume
If v <> Nil Then
virtualFile = v.Root.Child(s)
If virtualFile <> Nil Then
Var stream As BinaryStream
stream = BinaryStream.Open(virtualFile, False)
Do
TextArea2.AddText(stream.Read(255))
Loop Until stream.EndOfFile
stream.Close
End If
End If
End If
End Sub

See Also

FolderItem.DriveCount function; FolderItem class.