Working with Files

From Xojo Documentation

These questions are related to file handling, which make use of the FolderItem class.

How do I load and save data to a file?

There are several classes to help you load and save your data. If you are working with text files, you use the TextInputStream class to load data and the TextOutputStream class to save data. If you are working with binary data (or data with a fixed-size file format) you can use the BinaryStream class.

This code prompts the user to choose a file and then its contents are read and displayed in a text area:

Dim f As FolderItem = GetOpenFolderItem("")
If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open could raise an exception
Dim t As TextInputStream
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8
TextArea1.Text = t.ReadAll
Catch e As IOException
MsgBox("Error accessing file.")
End Try
End If
End If

This code saves text in a TextArea to a text file in the Documents folder:

Dim Documents As FolderItem = SpecialFolder.Documents
If Documents <> Nil Then
Dim f As FolderItem = Documents.Child("Sample.txt")
If f <> Nil Then
// TextOutputStream.Create raises an IOException if it can't open the file for some reason.
Dim t As TextOutputStream = TextOutputStream.Create(f)
t.Write(ConvertEncoding(TextField1.Text, Encodings.UTF8))
Catch e As IOException
// handle
End Try
End If
End If

How do I list files in a folder?

The FolderItem class is used to access the file system. You can use its Count property and Item method to iterate through files contained in a folder.

This code puts the name of all files on the Desktop into a ListBox:

Dim file As FolderItem
Dim folder As FolderItem = SpecialFolder.Desktop
Dim fileCount As Integer = folder.Count
For i As Integer = 1 To fileCount
file = folder.Item(i)
If file <> Nil Then
End If

How do I upload and download files in a web app?

To allow users to upload files to you web app, you use the WebFileUploader control. This control provides a user interface for the user to select and add files to a list to be uploaded. You then add a button to the page that calls the WebFileUploader.Upload method on the control to actually upload the files to your web app on the server. When the upload has finished, the WebFileUploader.UploadComplete event handler is called where you can get a list of the files that were uploaded so that you can process them as necessary.

To make a file available for download, you first have to create a WebFile that refers to the file on the drive. This WebFile should be a Property of the WebPage so that it does not go out of scope while the file is downloading, which would cause an incomplete download. You then want to set the WebFile.ForceDownload property to True so that the file is downloaded rather than displayed in the browser. To start the download, you call ShowURL and pass in the URL of the WebFile that you created.

For more information:

  • Examine the WebFileUploader example project (Examples/Web/Controls) that shows you how to upload files to a web app.
  • Examine the Downloading example project (Examples/Web/Downloading) that shows you how to download files from a web app.
  • Watch the Uploading and Downloading Files with a Web App video.

How do I create a folder?

To create a folder, you first create a FolderItem that points to where you want the folder. Then you call the FolderItem.CreateAsFolder method.

This code creates a folder called "MyData" in the Documents folder:

Dim myData As FolderItem = SpecialFolder.Documents.Child("MyData")