Skip Navigation LinksBase4.NET : Quickstarts : Cool Stuff : Save a file into Base4?

Skip Navigation Links.

Save a File into Base4?

Base4 ships with several useful classes in the built-in schema, which provide the building blocks of Base4. One of the most useful is the FileBase class, which can be used immediately for storing Files inside a Base4 data store. In this example we take a copy of a file off disk and put it directly in Base4, from where we can retrieve it again later.

 

using System;

using System.Collections.Generic;

using System.Windows.Forms;

using Base4.Storage;

namespace Base4.Storage.Quickstart

{

    static class Program

    {

        /// <summary>

        /// Put a copy of the file identified by the provided filePath into

        /// Base4, under the Root folder.

        /// Then delete it! I.e. do something pretty pointless!!!

        /// </summary>

        static void PutAFileInBase4(string filePath)

        {

            FileInfo info = new FileInfo(filePath);

            if (info.Exists)

            {

                FileBase file = new FileBase();

                file.Name = info.Name;

                file.FileContent.Content = File.ReadAllBytes(filePath);

                file.ContainedBy.Add(StorageContext.FindOne<FileBase>(FileBase.Fields.Name == "Root"));

                file.Save();

 

                file = StorageContext.FindOne<FileBase>(

                                FileBase.Fields.ContainedBy.Name == "Root" &&

                                FileBase.Fields.Name == info.Name

                );

                file.Delete();

            }

        }

    }

}

 

As you can see this is very simple.

 

It is also worth noting that the FileBase class itself is very lightweight, the BYTES associate with the File are not loaded with the FileBase, instead only when the FileContent property is accessed is the actual File loaded into memory. This allows you to do things like directory listings very efficiently without unnecessary performance overhead.