Skip to content

Create Open the embedded file system.

didisoft edited this page Aug 19, 2021 · 2 revisions

Create/Open the embedded file system.

The access to the embedded file system provided by DidiSoft EmbeddedFS is performed via the constructors of the EmDriveInfo class. This article illustrates how to create or open an embedded file system.

Table of contents

Create/Open embedded file system in the RAM memory

An embedded file system contained in the RAM memory will live until the calling application runs. If we want to persist it we can do so with the EmDriveInfo.SaveTo methods.

using EmbeddedFS;
...
EmDriveInfo drive = new EmDriveInfo();
// load from a previously persisted location
drive.LoadFrom(@"c:\Data\embedded.fs");
// persist the data if needed
drive.SaveTo(@"c:\Data\embedded.fs");

using EmbeddedFS; ... EmDriveInfo drive = new EmDriveInfo(); // load from a previously persisted location drive.LoadFrom(@"c:\Data\embedded.fs"); // persist the data if needed drive.SaveTo(@"c:\Data\embedded.fs");

Create/Open embedded file system inside a file

EmDriveInfo drive = new EmDriveInfo(@"c:\Temp\drive2.efs");

EmDriveInfo drive = new EmDriveInfo(@"c:\Temp\drive2.efs");

Create/Open embedded file system file protected with a password

A password-protected embedded file system file is AES-256 encrypted with a key derived from the password.

EmDriveInfo drive = new EmDriveInfo(@"c:\Temp\drive2.efs", "my password");

EmDriveInfo drive = new EmDriveInfo(@"c:\Temp\drive2.efs", "my password");

Check if a file password protected

Here is how to check if an embedded file system file is password protected

bool hasPassword = EmDriveInfo.IsPasswordProtected(@"c:\Temp\drive2.efs");
// check if a password mathes the password of an embedded file system
bool passwordMatches = EmDriveInfo.CheckPassword(@"c:\Temp\drive2.efs", "pass 123");

bool hasPassword = EmDriveInfo.IsPasswordProtected(@"c:\Temp\drive2.efs"); // check if a password mathes the password of an embedded file system bool passwordMatches = EmDriveInfo.CheckPassword(@"c:\Temp\drive2.efs", "pass 123");

Change the password of a file system

The password of an embedded file system file can also be changed

// set a password for an unprotected file system
EmDriveInfo.SetPassword(@"c:\Temp\drivewithpass.efs", "my password");
// change the password of a file system already protected with a password
EmDriveInfo.ChangePassword(@"c:\Temp\drivewithpass.efs", "old password", "new password");

// set a password for an unprotected file system EmDriveInfo.SetPassword(@"c:\Temp\drivewithpass.efs", "my password"); // change the password of a file system already protected with a password EmDriveInfo.ChangePassword(@"c:\Temp\drivewithpass.efs", "old password", "new password");

The password change methods and the EmDriveInfo constructor that opens the file system from a file will throw EmbeddedFS.Exceptions.PassworrdException when the provided opening password doesn’t match. In that case, it is a good practice to enclose our code with try/catch:

// set a password for an unprotected file system
try {
  // change the password of a file system already protected with a password
  EmDriveInfo.ChangePassword(@"c:\Temp\drivewithpass.efs", "old password", "new password");
} catch (EmbeddedFS.Exceptions.PasswordException)
{
  Console.WriteLine("The provided password doesn't match")
}

// set a password for an unprotected file system try { // change the password of a file system already protected with a password EmDriveInfo.ChangePassword(@"c:\Temp\drivewithpass.efs", "old password", "new password"); } catch (EmbeddedFS.Exceptions.PasswordException) { Console.WriteLine("The provided password doesn't match") }

Properties of an EmDriveInfo instance

An embedded file system instance contains a set of self-explanatory properties similar to those available in System.IO.DriveInfo. Probably the most used one is RootDirectory.

Console.WriteLine(drive2.AvailableFreeSpace);
Console.WriteLine(drive2.DriveFormat);
Console.WriteLine(drive2.FullName);
Console.WriteLine(drive2.InMemory);
Console.WriteLine(drive2.RootDirectory);
Console.WriteLine(drive2.TotalFreeSpace);
Console.WriteLine(drive2.TotalSize);
Console.WriteLine(drive2.VolumeLabel);

Console.WriteLine(drive2.AvailableFreeSpace); Console.WriteLine(drive2.DriveFormat); Console.WriteLine(drive2.FullName); Console.WriteLine(drive2.InMemory); Console.WriteLine(drive2.RootDirectory); Console.WriteLine(drive2.TotalFreeSpace); Console.WriteLine(drive2.TotalSize); Console.WriteLine(drive2.VolumeLabel);

Root Directory

A newly created embedded file system always contains a Root Directory.

Console.WriteLine(drive2.AvailableFreeSpace);
EmDirectoryInfo rootDir = drive2.RootDirectory;

Console.WriteLine(drive2.AvailableFreeSpace); EmDirectoryInfo rootDir = drive2.RootDirectory;

Summary

This page is a starting point for using the embedded file system API offered by EmbeddedFS.dll.

Probably from here, you can visit the chapters for working with files and folders.