Before using a directory, you must first have it. You
can use an existing directory if the operating system or someone else had
already created one. You can also create a new directory. Directories are
created and managed by various classes but the fundamental class is
Directory. Directory is an abstract and sealed class. All of its
member functions are static, which means you will never need to declare an
instance of the Directory class in order to use it.
Besides the Directory class, additional
operations of folders and sub-folders can be performed using the
DirectoryInfo class.
To create a directory, you can call the
CreateDirectory() member function of the Directory class. This
member function is available in two versions. One of the versions uses the
following syntax:
public:
static DirectoryInfo ^ CreateDirectry(String ^path);
This member function takes as argument the (complete)
path of the desired directory. Here is an example:
E:\Programs\Business Orders\Customer Information
When this member function is called:
- It first checks the parent drive, in this case E.
If the drive
doesn't exist, because this member function cannot create a drive, the
compiler would throw a DirectoryNotFoundException exception
- If the drive (in this case E) exists, the compiler moves to the
first directory part of the path; in this case this would be the
Programs folder in the E drive.
If the folder doesn't exist, the
compiler would create it. If that first director doesn't exist, this
means that the other directory(ies), if any, under the first don't
exist. So, the compiler would create it/them
- If the first directory exists and if there is no other directory
under that directory, the compiler would stop and would not do anything
further.
- If the directory exists and there is a sub-directory specified under
it, the compiler would check the existence of that directory.
If the
sub-directory exists, the compiler would not do anything further and
would stop. If the sub-directory doesn't exist, the compiler would
create it
- The compiler would repeat step 4 until the end of the specified path
The Directory::CreateDirectory() member function
returns a DirectoryInfo object that you can use as you see fit.
Practical
Learning: Creating a Directory
|
|
- On the (main) form, double-click an unoccupied area of its body
- Implement its Load event as follows:
System::Void Form1_Load(System::Object^ sender, System::EventArgs^ e)
{
String ^ strDirectory = L"C:\\Watts A Loan";
if( !Directory::Exists(strDirectory) )
Directory::CreateDirectory(strDirectory);
String ^ strFilename = strDirectory + L"\\Employees.wal";
FileInfo ^ fiEmployees = gcnew FileInfo(strFilename);
// If the employees file was not created already,
// then create it
if( !fiEmployees->Exists )
{
StreamWriter ^ stwEmployees = fiEmployees->CreateText();
// And create a John Doe employee
try {
stwEmployees->WriteLine(L"00-000");
stwEmployees->WriteLine(L"John Doe");
}
finally
{
stwEmployees->Close();
}
}
}
- Display the NewEmployee form and double-click its Create button
- Implement its Click event as follows:
System::Void btnCreate_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^ strFilename = L"C:\\Watts A Loan\\Employees.wal";
FileInfo ^ fiEmployees = gcnew FileInfo(strFilename);
StreamWriter ^ stwEmployees = nullptr;
// Normally, we should have the file already but just in case...
if( !fiEmployees->Exists )
stwEmployees = fiEmployees->CreateText();
else // If the file exists already, then we will only add to it
stwEmployees= fiEmployees->AppendText();
try {
stwEmployees->WriteLine(txtEmployeeNumber->Text);
stwEmployees->WriteLine(txtEmployeeName->Text);
}
finally
{
stwEmployees->Close();
}
txtEmployeeNumber->Text = L"";
txtEmployeeName->Text = L"";
txtEmployeeNumber->Focus();
}
- Return to the (main) form
Checking for a Directory Existence
|
|
Before using or creating a directory, you can first
check if it exists. This is because, if a directory already exists in the
location where you want to create it, you would be prevented from creating
one with the same name. In the same way, if you just decide to directly use
a directory that doesn't exist, the operation you want to perform may fail
because the directory would not be found.
To check whether a directory exists or not, you can call
the Directory::Exists() Boolean static member function. Its syntax
is:
public:
static bool Exists(String ^path);
This member function receives the (complete) path of the
directory. If the path exists, the member function returns true. If the
directory doesn't exist, the member function returns false.
To create a directory, you can call the
CreateDirectory() member function of the Directory class.
One of the most routine operations performed in a
directory consists of looking for a file. Both Microsoft Windows operating
systems and the user's intuition have different ways of addressing this
issue. The .NET Framework also provides its own means of performing this
operation, through various techniques. You can start by checking the
sub-directories and files inside of a main directory.
To look for files in a directory, the DirectoryInfo
class can assist you with its GetFiles() member function, which is
overloaded with three versions.
Practical
Learning: Using Directories and Files
|
|
- In the combo box on top of the Properties window, select
txtAccountNumber
- In the Events section, double-click Leave and implement the event as
follows:
System::Void txtAccountNumber_Leave(System::Object^ sender, System::EventArgs^ e)
{
String ^ strPath = L"C:\\Watts A Loan";
DirectoryInfo ^ diLoans =
gcnew DirectoryInfo(strPath);
array<FileInfo ^> ^ aryLoans = diLoans->GetFiles(L"*",
SearchOption::AllDirectories);
String ^ strFilename = txtAccountNumber->Text + L".wal";
String ^ strFullname = strPath + L"none.wal";
bool found = false;
for each(FileInfo ^ fle in aryLoans)
{
if( fle->Name == strFilename )
{
found = true;
strFullname = fle->FullName;
}
}
if( found == true )
{
FileStream ^ stmLoans =
File::Open(strFullname,
FileMode::Open,
FileAccess::Read);
BinaryReader ^ bnrLoans = gcnew BinaryReader(stmLoans);
txtAccountNumber->Text = bnrLoans->ReadString();
txtCustomerName->Text = bnrLoans->ReadString();
txtEmployeeNumber->Text = bnrLoans->ReadString();
txtEmployeeName->Text = bnrLoans->ReadString();
txtLoanAmount->Text = bnrLoans->ReadString();
txtInterestRate->Text = bnrLoans->ReadString();
txtPeriods->Text = bnrLoans->ReadString();
txtMonthlyPayment->Text = bnrLoans->ReadString();
bnrLoans->Close();
stmLoans->Close();
}
}
- In the combo box on top of the Properties window, select
txtEmployeeNumber
- On the Properties window, click the Events button and double-click
Leave
- Implement the event as follows:
System::Void txtEmployeeNumber_Leave(System::Object^ sender, System::EventArgs^ e)
{
String ^ strFilename = L"C:\\Watts A Loan\\Employees.wal";
FileInfo ^ fiEmployees = gcnew FileInfo(strFilename);
if(fiEmployees->Exists )
{
if( txtEmployeeNumber->Text == L"" )
{
txtEmployeeName->Text = L"";
return;
}
else
{
StreamReader ^ strEmployees = fiEmployees->OpenText();
String ^ strEmployeeNumber, ^ strEmployeeName;
bool found = false;
try {
while( strEmployeeNumber = strEmployees->ReadLine() )
{
if( strEmployeeNumber == txtEmployeeNumber->Text )
{
strEmployeeName = strEmployees->ReadLine();
txtEmployeeName->Text = strEmployeeName;
found = true;
}
}
// When the application has finished checking the file
// if there was no employee with that number, let the user know
if( found == false )
{
MessageBox::Show(L"No employee with that number was found");
txtEmployeeName->Text = L"";
txtEmployeeNumber->Focus();
}
}
finally
{
strEmployees->Close();
}
}
}
}
- Return to the form and double-click the Save button
- Implement the event as follows:
System::Void btnSave_Click(System::Object^ sender, System::EventArgs^ e)
{
String ^ strPath = L"C:\\Watts A Loan\\" + txtAccountNumber->Text + L".wal";
FileStream ^ stmLoan = File::Create(strPath);
BinaryWriter ^ bnwLoan =
gcnew BinaryWriter(stmLoan);
try
{
bnwLoan->Write(txtAccountNumber->Text);
bnwLoan->Write(txtCustomerName->Text);
bnwLoan->Write(txtEmployeeNumber->Text);
bnwLoan->Write(txtEmployeeName->Text);
bnwLoan->Write(txtLoanAmount->Text);
bnwLoan->Write(txtInterestRate->Text);
bnwLoan->Write(txtPeriods->Text);
bnwLoan->Write(txtMonthlyPayment->Text);
txtAccountNumber->Text = L"";
txtCustomerName->Text = L"";
txtEmployeeNumber->Text = L"";
txtEmployeeName->Text = L"";
txtLoanAmount->Text = L"";
txtInterestRate->Text = L"";
txtPeriods->Text = L"";
txtMonthlyPayment->Text = L"";
txtAccountNumber->Focus();
}
finally
{
bnwLoan->Close();
stmLoan->Close();
}
}
- Execute the application to test it
- First create a few employees as follows:
Employee # |
Employee Name |
42-806 |
Patricia Katts |
75-148 |
Helene Mukoko |
36-222 |
Frank Leandro |
42-808 |
Jennie Lenney |
- Process a few loans
- Close the application
|
|