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.
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: Header File: Exercise.h #pragma once #using <System.dll> #using <System.Drawing.dll> #using <System.Windows.Forms.dll> using namespace System; using namespace System::IO; using namespace System::Drawing; using namespace System::Windows::Forms; public ref class CExercise : public Form { public: CExercise(void); private: void InitializeComponents(); }; Source File: Exercise.cpp #include <windows.h> #include "Exercise.h" CExercise::CExercise(void) { InitializeComponents(); } void CExercise::InitializeComponents() { Text = L"File-Based Application"; StartPosition = FormStartPosition::CenterScreen; Directory::CreateDirectory("D:\\Bethesda Car Rental"); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise()); return 0; } To create a folder using the DirectoryInfo class, call its Create() method that comes in two versions. Here is an example: #include <windows.h> #include "Exercise.h" CExercise::CExercise(void) { InitializeComponents(); } void CExercise::InitializeComponents() { Text = L"File-Based Application"; StartPosition = FormStartPosition::CenterScreen; DirectoryInfo ^ dirInfo = gcnew DirectoryInfo(L"D:\\Bethesda Car Rental"); dirInfo->Create(); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise()); return 0; } 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 means 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: #include <windows.h> #include "Exercise.h" CExercise::CExercise(void) { InitializeComponents(); } void CExercise::InitializeComponents() { Text = L"File-Based Application"; StartPosition = FormStartPosition::CenterScreen; if( Directory::Exists("D:\\Bethesda Car Rental") ) MessageBox::Show(L"The directory exists already"); else MessageBox::Show(L"That directory was not yet created"); } int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { Application::Run(gcnew CExercise()); return 0; } 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.
|
|
|||||||||||
|