Using Data Files on Devices

This topic includes the following sections:

 

With device platforms, there is no “current directory.” Whether you create files on a device or copy them to a device, files must be in a location on the system that allows read/write access. You can then use SETDFN to specify this as the default location and to specify an extension for data files used by your device application.

Terabyte files are not supported on devices. In addition to files created with the TBYTE option, the following are created as terabyte files:

If you want to use one of these types of file on a device, you must explicitly define the file as a non-terabyte file—e.g., by using NOTBYTE with bldism or no48 with the address keyword for XDL.

Using data files for Universal Windows Platform

For Universal Windows Platform (UWP) applications, there are several data locations you can use for files—for example, the Roaming data store, the Local data store, and the Temporary data store. See Microsoft documentation on user and app data stores for more information, and note that for data files deployed with the application, you must put the files in a folder in the application project and then copy the files to the Local app data store.

1. In Solution Explorer, create a folder for the data files in the application project.
2. Copy the data files (e.g., .ism and .is1) to the folder you just created.
3. Set the Build Action property for each data file to Content.
4. Add code to the application project to copy the data files from the Dat folder to the Local app data store. For example:
   data datFolder, @StorageFolder, await Windows.ApplicationModel.Package.Current.InstalledLocation.GetFolderAsync("Assets\Dat")
   data localFolder, @StorageFolder, ApplicationData.Current.LocalFolder
   await CopyAsync(datFolder, localFolder) ;See CopyAsync code below.
   .
   .
   .
public static async method CopyAsync, @Task
   source, @StorageFolder 
   destination, @StorageFolder 
   endparams
proc
   data targetFolder, @Windows.Storage.StorageFolder
   targetFolder = await destination.CreateFolderAsync(source.DisplayName, CreationCollisionOption.ReplaceExisting)
   data files, @IReadOnlyList<StorageFile>
      files = await source.GetFilesAsync(CommonFileQuery.DefaultQuery)                  
   data storageFile, @StorageFile
   foreach storageFile in files
   begin
      await storageFile.CopyAsync((@StorageFolder)targetFolder, storageFile.Name, NameCollisionOption.ReplaceExisting)
   end                  
endmethod
5. You can then use SETDFN to set a default location and extension for data files. For example:
xcall setdfn((A)(localFolder+".ism"),1)
open(1, "i:i", "syntxt")
close 1

Using data files on Android

For Android applications, you’ll put the data files in the Assets folder for the project, get the location of internal storage or the primary external storage directory for the device, and then copy the files to this directory.

1. Add the data files (e.g., .ism and .is1 files) to the Assets folder for the project that accesses the files, unless the files will be accessed over a network or created by the application.
2. Select the files in Solution Explorer (under Assets), view file properties, and check the Build Action for the files. This should be set to AndroidAsset.
3. Add code to copy the files from the deployed package (.apk) to internal storage or to the primary external storage directory for the device. Use the BaseContext property for the Activity to get the location of internal storage, or use the Xamarin.Android Framework property Android.OS.Environment.ExternalStorageDirectory to get the primary external storage directory. You must have read/write access to the directory you use. (The application may crash if the directory is read-only.)

For example:

; Copy the ism file to internal storage. dbIsm and dbIs1 are the names of the 
; ism files, and "this" indicates that this code is inside an Activity class. 
data ismPath,   String
; "import System.IO" is required for Path.Combine.
ismPath = Path.Combine(this.BaseContext.FilesDir.AbsolutePath, dbIsm)
data is1Path,   String
is1Path = Path.Combine(this.BaseContext.FilesDir.AbsolutePath, dbIs1)
data br = new BinaryReader(Assets.Open(dbIsm))
data bw = new BinaryWriter(new FileStream(ismPath, FileMode.Create))
			
data myBuffer,  [#]byte,   new Byte [100]
data len,       int
			
if (File.Exists(ismPath))
begin
    while ((len = br.Read(myBuffer, 0, myBuffer.Length)) > 0)
    begin
        bw.Write(myBuffer, 0, len)
    end
end
			
bw.Flush()
br.Close()
bw.Close()
			
; Copy the is1 file to internal storage
br = new BinaryReader(Assets.Open(dbIs1))
bw = new BinaryWriter(new FileStream(is1Path, FileMode.Create))
			
if (File.Exists(is1Path))
begin
    while ((len = br.Read(mybuffer, 0, myBuffer.Length)) > 0)
    begin
        bw.Write(mybuffer, 0, len)
    end
end
			
bw.Flush()
br.Close()
bw.Close()
4. You can then use SETDFN to set a default location and extension for data files. For example:
xcall setdfn((A)(ismPath+".ism"),1)
open(1, "i:i", "syntxt")
close 1

As an alternative, you can use the Android Device Monitor to deploy files to an Android emulator or an Android device that’s configured for development and connected to the Windows development machine (see Building, debugging, and deploying Android applications for information).

1. Open the Android Device Monitor, which is included in the Android SDK tools.
2. On the Devices tab of the Android Device Monitor, and select the Android emulator or device.
3. On the File Explorer tab, select the data files (or a folder with the data files) you want to deploy. Then click the “Push file onto device” toolbar button: 

Using data files on iOS

For iOS, put the data files in the Resources folder for the application project, copy the files to the Documents folder on the device, and then set SETDFN to this directory and the file extension(s):

1. Create a Resources folder in the application project, if one doesn’t already exist.
2. Put the data files (e.g., .ism and .is1) in the Resources folder.
3. Make sure the Build Action property for each data file is set to BundleResource.
4. Add code to copy the files to the “Documents” folder where the application has read/write access. (Without this code, the data files will be placed in the application’s root directory, which is restricted to read-only operations.) For example, to deploy a file named syntext.ism with your application, you could use the following:
;Copy syntxt.ism from application's root directory to "Documents"
DATA doc=Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments)
DATA ismPath = System.IO.Path.Combine(doc, "syntxt.ism")
File.Copy("./syntxt.ism", ismPath, true)
			
;Copy syntxt.is1 from application's root directory to "Documents"
var is1Path = Path.Combine(doc, "syntxt.is1")
File.Copy("./syntxt.is1", is1Path, true)
5. You can then use SETDFN to set a default location and extension for data files. For example:
xcall setdfn((A)(ismPath+".ism"),1)
open(1, "i:i", "syntxt")
close 1