NativeFileSO

Keiwando.NFSO

class

NativeFileSO

public class NativeFileSO : INativeFileSO 

Summary

Provides methods for native file import and export functionality which is shared between both mobile and desktop platforms.

Examples

The following example demonstrates how to use the NativeFileSO class in order to copy/export a file from an existing path to a new location chosen by the user.

 using Keiwando.NFSO;
 
 public class SaveTest {

 	public static void Main() {

 		string path = "path/to/existing/fileToSave.txt";
 		string newFilename = "ExportedFile.txt";
			
 		FileToSave file = new FileToSave(path, newFilename, SupportedFileType.PlainText);
 		
 		// Allows the user to choose a save location and saves the
 		// file to that location
 		NativeFileSO.shared.SaveFile(file);
 	}
 }

The following example demonstrates how to use the NativeFileSO class in order to let the user choose a text file and handle its loaded contents.

 using Keiwando.NFSO;
 
 public class OpenTest {
 	
 	public static void Main() {
 		
 		// We want the user to select a plain text file.
 		SupportedFileType[] supportedFileTypes = {
 			SupportedFileType.PlainText
 		};
 		
 		NativeFileSO.shared.OpenFile(supportedFileTypes, 
 									 delegate(bool fileWasOpened, OpenedFile file) {
 			if (fileWasOpened) {
 				// Process the loaded contents of "file" e.g
 				// using OpenedFile.ToUTF8String()
 			} else {
 				// The file selection was cancelled.
 			}
 		});
 	}
 }

Remarks

The dialogs shown by the Open and Save functions block any other UI interactions until the user dismisses the dialog.

This class is currently compatible with Windows, macOS, iOS and Android. Attempting to call the class methods on unsupported platforms will result in a NullReferenceException.

Thread safety is not guaranteed!

Properties

public static readonly NativeFileSO shared

The shared instance through which the API should be accessed.

Methods

public void OpenFile (SupportedFileType[], Action<bool, OpenedFile>)

Presents a native dialog to the user which allows them to select a single file to be opened. The selected file contents are then loaded into memory managed by an instance of the OpenedFile class.

public void OpenFiles (SupportedFileType[], Action<bool, OpenedFile[]>)

Presents a native dialog to the user which allows them to select multiple files to be opened at once. The selected file contents are then loaded into memory managed by instances of the OpenedFile class.

public void SaveFile (FileToSave)

Presents a native dialog to the user which allows them to select an export location for the specified file and exports/copies the file to that location.