Home

The Directory of a Database

 

Introduction

One of the biggest differences between a database and a regular application is that, traditionally, although not always, all of the files of a database are located in the same directory. The directory can be local and accessed only by one computer. The directory can be located in one computer and accessed by various users on different computers or a workgroup. The directory can be located on a server that no user directly uses but that directory's files can be accessed from one or more computers.

Another particularity of a database is that usually you, the database developer, create and manage the directory or directories used by the application.

Another difference of a database as compared to a regular application is that, while using the database, users do not create files. This means that there is no actual file processing on the part of the users. For example, the user does not even open the database in the traditional sense. You, the database developer, provides a means of accessing the database. Then, the user adds, edits, or deletes values.

Creating a Directory

Based on the above discussion of directories, when creating a file-based application, one the first actions you should perform consists of setting up the directory where the file(s) of your application would be located. If you already know (and you should know) where and how the application would be accessed, you can manually create a folder using Windows Explorer, My Computer, or any appropriate utility. Otherwise, you can still programmatically create the directory.

The .NET Framework supports the creation and management of directories through various classes. The main class used to deal with directories is called Directory. Besides the Directory class, the .NET Framework provides support for folders through a class named DirectoryInfo. To use it, declare a variable of type DirectoryInfo using its constructor to initialize it.

To actually create a directory using the static Directory class, you can call its CreateDirectory() method that is overloaded with two versions. Here is an example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace Exercise
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDirectory_Click(object sender, EventArgs e)
        {
            Directory.CreateDirectory(@"E:\Bethesda Car Rental");
        }
    }
}

To create a folder using the DirectoryInfo class, call its Create() method that comes in two versions. Here is an example:

private void btnDirectory_Click(object sender, EventArgs e)
{
    DirectoryInfo dirInfo = new DirectoryInfo(@"E:\Bethesda Car Rental");
    dirInfo.Create();
}

When you call either the Directory.CreateDirectory() method or the DirectoryInfo.Create() method, if the directory does not exist, it would be created. If the directory exists already, nothing would happen. This implies that the compiler would not attempt to create a directory if there is already one in the indicated location and you can safely call any of these methods without the risk of deleting its existing files, if any.

Before performing any operation on a directory, you should first make sure it exists. To get this information, you can call the Directory.Exists() method that returns a Boolean value. This method takes as argument the path to the directory. Here is an example:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace WinForms4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnDirectory_Click(object sender, EventArgs e)
        {
            if (Directory.Exists(@"E:\Bethesda Car Rental"))
                MessageBox.Show("The directory exists already");
            else
                MessageBox.Show("That directory was not yet created");
        }
    }
}

During the lifetime of your database, at one time, you may want to change its location, for any reason you judge necessary. Although this operation is primarily easily supported, it could become complex in some scenarios. Still, to move a directory and its contents, you can call the Directory.Move() method. This method takes two arguments: the source and the destination. After the method has been called, the directory held by the first argument would be moved, along with its sub-folders and files, to the path specified by the second argument. To move a directory using the DirectoryInfo class, you can call its MoveTo() method.

As opposed to creating a directory, if you don't need it anymore, you can remove it. To support this, the Directory class is equipped with the Delete() method that is overloaded with two versions. One of the versions is used to delete a directory that is currently empty while the other version is used to delete the directory and its content.

 

Previous Copyright © 2007 FunctionX, Inc. Next