用下面的代码可以创建多层目录 例如gCreateDir("C:\\dir1\\dir2\\dir3"); 如果只需要创建一层目录用 CreateDirectory 就可以了。 // Create a directory. // This function can create a directory under an unexist directory // but CreateDirectory() API can't. bool gCreateDir(CString csDir) { HANDLE fFile; // File Handle WIN32_FIND_DATA fileinfo; // File Information Structure CStringArray m_arr; // CString Array to hold Directory Structures BOOL tt; // BOOL used to test if Create Directory was successful int x1 = 0; // Counter CString tem = ""; // Temporary CString Object
// Before we go to a lot of work. // Does the file exist
fFile = FindFirstFile(csDir,&fileinfo);
// if the file exists and it is a directory if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { // Directory Exists close file and return FindClose(fFile); return TRUE; }
m_arr.RemoveAll(); // Not really necessary - Just habit for(x1=0;x1<csDir.GetLength();x1++) // Parse the supplied CString Directory String { if(csDir.GetAt(x1) != '\\') // if the Charachter is not a \ tem += csDir.GetAt(x1); // else add character to Temp String else { m_arr.Add(tem); // if the Character is a \ Add the Temp String to the CString Array tem += "\\"; // Now add the \ to the temp string } if(x1 == csDir.GetLength()-1) // If we reached the end of the file add the remaining string m_arr.Add(tem); }
// Close the file FindClose(fFile);
// Now lets cycle through the String Array and create each directory in turn for(x1 = 1;x1<m_arr.GetSize();x1++) { tem = m_arr.GetAt(x1); tt = CreateDirectory(tem,NULL);
// If the Directory exists it will return a false if(tt) SetFileAttributes(tem,FILE_ATTRIBUTE_NORMAL); // If we were successful we set the attributes to normal } m_arr.RemoveAll(); // Now lets see if the directory was successfully created fFile = FindFirstFile(csDir,&fileinfo);
// if the file exists and it is a directory if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { // Directory Exists close file and return FindClose(fFile); return TRUE; } else { FindClose(fFile); return FALSE; }
如果只需要创建一层目录用 CreateDirectory 就可以了。
// Create a directory.
// This function can create a directory under an unexist directory
// but CreateDirectory() API can't.
bool gCreateDir(CString csDir)
{
HANDLE fFile; // File Handle
WIN32_FIND_DATA fileinfo; // File Information Structure
CStringArray m_arr; // CString Array to hold Directory Structures
BOOL tt; // BOOL used to test if Create Directory was successful
int x1 = 0; // Counter
CString tem = ""; // Temporary CString Object
// Before we go to a lot of work.
// Does the file exist
fFile = FindFirstFile(csDir,&fileinfo);
// if the file exists and it is a directory
if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
// Directory Exists close file and return
FindClose(fFile);
return TRUE;
}
m_arr.RemoveAll(); // Not really necessary - Just habit
for(x1=0;x1<csDir.GetLength();x1++) // Parse the supplied CString Directory String
{
if(csDir.GetAt(x1) != '\\') // if the Charachter is not a \
tem += csDir.GetAt(x1); // else add character to Temp String
else
{
m_arr.Add(tem); // if the Character is a \ Add the Temp String to the CString Array
tem += "\\"; // Now add the \ to the temp string
}
if(x1 == csDir.GetLength()-1) // If we reached the end of the file add the remaining string
m_arr.Add(tem);
}
// Close the file
FindClose(fFile);
// Now lets cycle through the String Array and create each directory in turn
for(x1 = 1;x1<m_arr.GetSize();x1++)
{
tem = m_arr.GetAt(x1);
tt = CreateDirectory(tem,NULL);
// If the Directory exists it will return a false
if(tt)
SetFileAttributes(tem,FILE_ATTRIBUTE_NORMAL);
// If we were successful we set the attributes to normal
}
m_arr.RemoveAll();
// Now lets see if the directory was successfully created
fFile = FindFirstFile(csDir,&fileinfo);
// if the file exists and it is a directory
if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
// Directory Exists close file and return
FindClose(fFile);
return TRUE;
}
else
{
FindClose(fFile);
return FALSE;
}
}
使用API函数也可以!
mkdir("c:\\test");