Skip to content

FileBrowser Class

kalucky0 edited this page Jun 11, 2023 · 5 revisions

The FileBrowser class provides methods for opening file browsers and selecting files or folders. It uses platform-specific native libraries to interact with the operating system's file browser.

Methods

OpenFileBrowser(string title = "", string directory = "", string[] extensions = null, bool multiple = false)

Opens a file browser dialog to select one or more files.

Parameters

  • title (string, optional): The title of the file browser dialog.
  • directory (string, optional): The directory to open the file browser in.
  • extensions (string[], optional): An array of file extensions that can be selected.
  • multiple (bool, optional): Specifies whether multiple files can be selected. Defaults to false.

Returns

  • string[]: An array of paths to the selected files.

 

OpenFileBrowser(string title, string[] extensions, bool multiple = false)

Opens a file browser dialog to select one or more files.

Parameters

  • title (string): The title of the file browser dialog.
  • extensions (string[]): An array of file extensions that can be selected.
  • multiple (bool, optional): Specifies whether multiple files can be selected. Defaults to false.

Returns

  • string[]: An array of paths to the selected files.

 

OpenFileBrowser(string[] extensions, bool multiple = false)

Opens a file browser dialog to select one or more files.

Parameters

  • extensions (string[]): An array of file extensions that can be selected.
  • multiple (bool, optional): Specifies whether multiple files can be selected. Defaults to false.

Returns

  • string[]: An array of paths to the selected files.

 

OpenFolderBrowser(string title = "", string directory = "", bool multiple = false)

Opens a folder browser dialog to select one or more folders.

Parameters

  • title (string, optional): The title of the folder browser dialog.
  • directory (string, optional): The directory to open the folder browser in.
  • multiple (bool, optional): Specifies whether multiple folders can be selected. Defaults to false.

Returns

  • string[]: An array of paths to the selected folders.

 

SaveFileBrowser(string title = "", string directory = "", string defaultName = "", string[] extensions = null)

Opens a file browser dialog to select a destination for saving a file.

Parameters

  • title (string, optional): The title of the file browser dialog.
  • directory (string, optional): The directory to open the file browser in.
  • defaultName (string, optional): The default name of the file to save.
  • extensions (string[], optional): An array of file extensions that the file can be saved with.

Returns

  • string: The path to where the file should be saved.

 

Remarks

  • The OpenFileBrowser and OpenFolderBrowser methods return an empty string array (string[]) if no files or folders are selected.
  • The SaveFileBrowser method returns an empty string (string) if the user cancels the save operation.

Example Usage

// Open a file browser to select multiple image files
string[] selectedFiles = FileBrowser.OpenFileBrowser("Select Images", "C:/", new[] { ".jpg", ".png", ".gif" }, true);

foreach (string file in selectedFiles)
{
    Console.WriteLine("Selected File: " + file);
}

// Open a folder browser to select a folder
string[] selectedFolders = FileBrowser.OpenFolderBrowser("Select Folder", "C:/", false);

foreach (string folder in selectedFolders)
{
    Console.WriteLine("Selected Folder: " + folder);
}

// Open a file browser to select a location for saving a file
string savePath = FileBrowser.SaveFileBrowser("Save File", "C:/", "image.jpg", new[] { ".jpg" });

Console.WriteLine("Save Path: " + savePath);