Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating and Removing Directories using Win32 System Services API

Author: DevASP
Download Source Code : 649_CretaeRemoveDirectories.zip

In this article I will discuss the Win32 System Service API’s available for manipulating directories, such as for Creating and deleting directories.

Every current modern operating system uses hierarchical, or tree-structured, directories to store its files. A directory tree in Windows has its root at a drive letter, for example, C:\. This means that a Windows machine has multiple root directories, one for each drive letter. Each drive (or partition) can have a file system different from that of the other drives, so individual directory trees can have slightly different characteristics. For example, the old FAT file system has much smaller file names than does the Windows 2000 file system, and a FAT partition has a much smaller potential size than an NTFS partition. None of the three file systems native to Windows 2000 (FAT, NTFS, or HPFS) allow symbolic links, but other file systems may support this capability in the future. You can learn the characteristics for a given directory tree by using the drive information functions.In all file systems, a directory can hold zero or more entries. The en-tries can be either files, or other directories, Because of this tree-like structure, functions that walk directory trees tend to be recursive.

 

Creating and Deleting Directories:

Typically a user creates a directory either with the File Manager or with the mkdir command. There are many reasons that you may do the same thing inside of an application. For example, if you are writing an application that installs another application or a set of data files, you will need to create directories to hold the files that you are installing.The CreateDirectory API is used to create a new directory.

 

The signature of this API looks like this.

 

 

BOOL CreateDirectory(LPTSTR dirName,LPSECURITY_ATTRIBUTES security); 

 

 dirName - Name/path of the directory to create security - Security attributes.

 

The dirName parameter accepts either a name or a path. If it receives just a name, it forms the new directory as a child of the current directory. If it receives a path (e.g. "C:\dos\temp\new"), it traverses the path ("c:\dos\ temp") and creates the newdirectory ("new") there. If the path is invalid, it fails. The GetLastError function contains a detailed error code following any failure. It is just as easy to delete a directory, using the RemoveDirectory APi of windows system services.

 

The signature of this API looks like this

 

 

BOOL RemoveDirectory(LPTSTR dirName); 

 

dirName - Name/path of the directory to remove Returns TRUE on success.

 

The RemoveDirectory function can remove a directory only if it is empty. It accepts the same name and/or path information described for CreateDirectory above.The sample code attached with this article simply creates a directory at specified location and then removes it.The code for creating a directory looks like this. 

 

char Path[]="C:\\MyDirectory";

BOOL success;

 

success = CreateDirectory(Path, 0);

if (!success)

  {

      CString str;

      str.Format("Directory cant be reated due to the reason %s",GetLastError());

      MessageBox("Directory Can't be created ");

   }

   else

      MessageBox("Directory Has been  be created Successfully in c drive by named MyDirectory ");

 

This code will create a directory named “MuDirectory”  in the root of C Drive.And the code for deleting a specified directory looks like this.

 

char Path[]="C:\\MyDirectory";

BOOL success;

success = RemoveDirectory(Path);

   if (!success)

   {

      CString str;

      str.Format("Directory cant be removed due to the reason %s",GetLastError());

      MessageBox(str);

   }

   else

       MessageBox("Directory Successfully removed");

 

This will simply remove the directory created by the above code.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Creating a Switch menu.

Disclaimer - Privacy
© 2002-2017 DevASP.net