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

How to Copy, Move, and delete a file using win32 system services

Author: DevASP
Download Source Code : 625_Win32 system Services for Copy,move and delete

In this article I will describe you that how we can use the OS services to Copy Move and delete files. OS provides three system services for using them into our program to duplicate the command line functionality.

But before this I will discuss an API of Win32 System service named GetFullPathName.TheGetFullPathName function is simply a convenience. It appends the requested file name to the current path to form a complete path name. However, it does no checking on the resulting path name to ensure that it is valid.The signature of this is looks like this…


DWORD GetFullPathName(LPCTSTR fileName,DWORD bufferSize,

   LPTSTR buffer,LPTSTR filePart);


filename                                    The name of the file

bufferSize                                  Size of buffer for resulting path name

buffer                                        Buffer for the resulting path name

filePart                                      Pointer to the file name in the buffer


Returns the length of the full path name, or 0 on error

Simple code can be written to understand this API.


char filename[]=”TestFile.txt”;

   DWORD pathSize;

   char pathBuffer[200];

   char *address;


   pathSize = GetFullPathName(filename, 200,

      pathBuffer, &address);

   if (pathSize < 200)

      pathBuffer[pathSize] = 0;


      pathBuffer[0] = 0;

MessageBox(pathBuffer,"Full path");

MessageBox(address,"Only file name"); 


Copy Files

The CopyFile function copies the source file to the destination file name. If an error occurs during the copy,GetLastError contains the error code.



BOOL CopyFile(LPTSTR sourceFile,LPTSTR destFile,BOOL existFail);


sourceFile                        File name for the source file

destFile                           File name for the destination

existFail                          Determines if an existing destination file should be preserved

Returns TRUE on success.

The existFail parameter controls the behavior of the function when the destination file name already exists. If you set it to TRUE, the function fails when the destination file name already exists. When set to FALSE, the function overwrites an existing file.The code sample for copying a file



const int PATH_SIZE=200;

      char sourceFilename[]="TestFile.txt";

      char destFilename[PATH_SIZE]="D:\\TestFile.txt";

      BOOL success;


      success = CopyFile(sourceFilename, destFilename,


      if (!success)





Delete Files

The DeleteFile function deletes an existing file, provided that it is not open. It accepts the name of the file and deletes it immediately.



BOOL DeleteFile(LPTSTR fileName);


If the return value is FALSE, use the GetLastError function to retrieve the error code.The sample code to delete a file is like this




char filename[]="TestFile.txt";

      BOOL success;


      success = DeleteFile(filename);

      if (success)



            MessageBox("Cant delet the file");



Move Files

Win32 System Service provides two API’s for move file .One is simple version and another is enhanced version. The simple version moves a file from one name to another on a single volume. It simply changes the pointers in the directory structure, so it is fast. However, it does not allow movement across volumes or between different drive letters. The enhanced version allows files to move across volumes by copying and then deleting the original file, and it can also protect existing files or delay movement.The signature of this function looks like this


BOOL MoveFileEx( LPTSTR sourceFile,LPTSTR destFile,DWORD flags);


sourceFile                       Source file name

destFile                           Destination file name

Flags                              Flags that control how the move is performed

Returns TRUE on success.

Different flags can be used for this API MOVEFILE_REPLACE_EXISTING this flag determines whether or not the function deletes an existing destination file during the move.MOVEFILE_COPY_ALLOWED determines whether cross-volume moves are allowed. MOVEFILE_DELAY_UNTIL_RE-BOOT this API works only in Windows 2000. It sets up for the move but does not actually perform it until the very beginning of the next reboot. This is handy for moving operating system files.The source code for moving a file looks like this.


char sourceFilename[]="TestFile.txt";

      char destFilename[]="E:\\";

      BOOL success;


      success = MoveFileEx(sourceFilename,

            destFilename, MOVEFILE_REPLACE_EXISTING );

      if (!success)

            MessageBox("Move Successfully");


            MessageBox("cant Move Successfully");



This will move your file and I think its sufficient to digest…

Add Article Comment:
Name :
Email Address :
Comments :
<< Getting and Setting File Attributes

Disclaimer - Privacy
© 2002-2017