有很多办法可以判断:可以使用:PathFileExists Function MSDN: BOOL PathFileExists( LPCTSTR pszPath ); Determines whether a path to a file system object such as a file or directory is valid.
// Invalid file path name (file is not there). char buffer_2[ ] = "C:\\TEST\\file.doc"; char *lpStr2; lpStr2 = buffer_2;
// Return value from "PathFileExists". int retval;
// Search for the presence of a file with a true result. retval = PathFileExists(lpStr1); if(retval == 1) { cout << "Search for the file path of : " << lpStr1 << endl; cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl; cout << "The return from function is : " << retval << endl; }
else { cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl; cout << "The return from function is : " << retval << endl; }
// Search for the presence of a file with a false result. retval = PathFileExists(lpStr2);
if(retval == 1) { cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl; cout << "Search for the file path of : " << lpStr2 << endl; cout << "The return from function is : " << retval << endl; } else { cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl; cout << "The return from function is : " << retval << endl; } }OUTPUT ============== Search for the file path of : C:\TEST\file.txt The file requested "C:\TEST\file.txt" is a valid file The return from function is : 1The file requested "C:\TEST\file.doc" is not a valid file The return from function is : 0枚举进程EnumProcess或者CreatToolHelpSnap32函数,MSDN下的例子#include <windows.h> #include <stdio.h> #include <tchar.h> #include <psapi.h>void PrintProcessNameAndID( DWORD processID ) { TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, processID ); // Get the process name. if (NULL != hProcess ) { HMODULE hMod; DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod), &cbNeeded) ) { GetModuleBaseName( hProcess, hMod, szProcessName, sizeof(szProcessName)/sizeof(TCHAR) ); } } // Print the process name and identifier. _tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID ); CloseHandle( hProcess ); }void main( ) { // Get the list of process identifiers. DWORD aProcesses[1024], cbNeeded, cProcesses; unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) ) return; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); // Print the name and process identifier for each process. for ( i = 0; i < cProcesses; i++ ) if( aProcesses[i] != 0 ) PrintProcessNameAndID( aProcesses[i] ); }
MSDN:
BOOL PathFileExists( LPCTSTR pszPath
);
Determines whether a path to a file system object such as a file or directory is valid.
===========
进程和目录,这两个好像扯不上边吧。判断目录是否存在,用CFile打开这个目录名,看返回值就行了。判断进程恐怕要枚举进程才行吧。
#include <iostream.h>
#include "Shlwapi.h"void main(void)
{
// Valid file path name (file is there).
char buffer_1[ ] = "C:\\TEST\\file.txt";
char *lpStr1;
lpStr1 = buffer_1;
// Invalid file path name (file is not there).
char buffer_2[ ] = "C:\\TEST\\file.doc";
char *lpStr2;
lpStr2 = buffer_2;
// Return value from "PathFileExists".
int retval;
// Search for the presence of a file with a true result.
retval = PathFileExists(lpStr1);
if(retval == 1)
{
cout << "Search for the file path of : " << lpStr1 << endl;
cout << "The file requested \"" << lpStr1 << "\" is a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
else
{
cout << "\nThe file requested " << lpStr1 << " is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
// Search for the presence of a file with a false result.
retval = PathFileExists(lpStr2);
if(retval == 1)
{
cout << "\nThe file requested " << lpStr2 << "is a valid file" << endl;
cout << "Search for the file path of : " << lpStr2 << endl;
cout << "The return from function is : " << retval << endl;
}
else
{
cout << "\nThe file requested \"" << lpStr2 << "\" is not a valid file" << endl;
cout << "The return from function is : " << retval << endl;
}
}OUTPUT
==============
Search for the file path of : C:\TEST\file.txt
The file requested "C:\TEST\file.txt" is a valid file
The return from function is : 1The file requested "C:\TEST\file.doc" is not a valid file
The return from function is : 0枚举进程EnumProcess或者CreatToolHelpSnap32函数,MSDN下的例子#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <psapi.h>void PrintProcessNameAndID( DWORD processID )
{
TCHAR szProcessName[MAX_PATH] = TEXT("<unknown>"); // Get a handle to the process. HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
PROCESS_VM_READ,
FALSE, processID ); // Get the process name. if (NULL != hProcess )
{
HMODULE hMod;
DWORD cbNeeded; if ( EnumProcessModules( hProcess, &hMod, sizeof(hMod),
&cbNeeded) )
{
GetModuleBaseName( hProcess, hMod, szProcessName,
sizeof(szProcessName)/sizeof(TCHAR) );
}
} // Print the process name and identifier. _tprintf( TEXT("%s (PID: %u)\n"), szProcessName, processID ); CloseHandle( hProcess );
}void main( )
{
// Get the list of process identifiers. DWORD aProcesses[1024], cbNeeded, cProcesses;
unsigned int i; if ( !EnumProcesses( aProcesses, sizeof(aProcesses), &cbNeeded ) )
return; // Calculate how many process identifiers were returned. cProcesses = cbNeeded / sizeof(DWORD); // Print the name and process identifier for each process. for ( i = 0; i < cProcesses; i++ )
if( aProcesses[i] != 0 )
PrintProcessNameAndID( aProcesses[i] );
}