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

Searching for a file Using Win32 System Services

Author: DevASP
Download Source Code :

The SearchPath function searches for a file, either in the system path or in an arbitrary directory or directory tree.

The program in Sample code with this article starts by assigning  the name of the file to be found to a variable named “name”. This file name must be specific—it cannot contain wild-card characters.The program then assigns the directory to a variable in which it will start the search named “pathName”. In this article we are going to search the “cards.dll” file from the directory “C:\\WINDOWS\\system32”. If you type "PATH" here, the program understands that command to mean that you want to search the system path. It places the value NULL in the pathPointer parameter, which tells the SearchPath function to search a specific set of directories in the order given below:


1-The directory containing the application's executable

2-The current directory

3-The Windows system directory

4-The Windows main directory

5-Any directories contained in the PATH environment variable


The function stops on the first match and returns the path to the file if it finds it. If it does not find the file, it returns an error code.The signature of this function looks like this. 



DWORD SearchPath(LPCTSTR path,LPCTSTR fileName,LPCTSTR fileExtension,

  DWORD bufferSize,LPTSTR buffer,LPTSTR *filePart); 



Path-                NULL for system path, or a string indicating the starting directory for the search.

Filename-         Name of file to find.

fileExtension-   File extension appended if fileName has none.

bufferSize-       Size of the buffer.

Buffer-             Buffer for path of found file.

filePart-           Pointer to start of filename in buffer.


Returns the length of the path found, or 0 on error.


The SearchPath function accepts in its first parameter either zero or a string indicating the starting directory. Zero implies that the system should search the system path for the file. The fileName parameter may or may not contain the file extension. If it does not, you can place an extension in the fileExtension parameter and the function will append it to the file name before starting the search. The bufferSize and buffer parameters pecify the buffer into which the function should return the result of the search, which is the path to the found file name. The filePart parameter returns a pointer to the first character of the actual file name in the buffer.If an error occurs during the search, the function returns a zero; the error code can be obtained using the GetLastError function. It is also possible that the buffer size is inadequate to hold the entire path name.In this case, the returned value will be greater than the bufferSize parameter passed to the function.The SearchPath function is a convenience. It returns the first instance of the file name requested. To do a more thorough search for duplicates, or to search for file names containing wildcards, you have to write code

to do the search yourself. 


The sample code looks like this 



char name[MAX_PATH]="cards.dll";

      char pathName[MAX_PATH]="C:\\WINDOWS\\system32"/*"PATH"*/;

      char *pathPointer;

      char *p;

      char buffer[MAX_PATH];

      DWORD len;


      // Search named path, or system path

      if (strcmp(pathName, "PATH") == 0)

            pathPointer = NULL;


            pathPointer = pathName;


      // do the search. It may take a while

      len = SearchPath(pathPointer, name, 0,MAX_PATH, buffer, &p);

      MessageBox("The search may take some time.....\nPress Ok to Voew Result");


      if (len == 0)

            MessageBox("Not Found");



            CString strMessage;

            strMessage.Format("Found in %s",buffer);





It simplay set file name and directory path where we have to search the file.Here I have set the directory path to "C:\\WINDOWS\\system32".You can also set it to “PATH” to see the other Behaviour of the Win32 System services API.So must check it, it will also search it because this directory is also a system directory.

Article Comments

Posted on 11/11/2006 2:08:29 AM by Ramesh Chowdary

Add Article Comment:
Name :
Email Address :
Comments :
<< Getting and Setting the Current Directory:

Disclaimer - Privacy
© 2002-2017