以下代码可创建多级目录BOOL MakeDirectory(CString dd) { 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 fFile = FindFirstFile(dd,&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(); for(x1 = 0; x1 < dd.GetLength(); x1++ ) // Parse the supplied CString Directory String { if(dd.GetAt(x1) != '\\') // if the Charachter is not a \ tem += dd.GetAt(x1); // add the character to the Temp String else { m_arr.Add(tem); // if the Character is a \ tem += "\\"; // Now add the \ to the temp string } if(x1 == dd.GetLength()-1) // If we reached the end of the 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 } // Now lets see if the directory was successfully created fFile = FindFirstFile(dd,&fileinfo); m_arr.RemoveAll(); if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY) { // Directory Exists close file and return FindClose(fFile); return TRUE; } else { // For Some reason the Function Failed Return FALSE FindClose(fFile); return FALSE; } }
to bohut CreateDirectory是API函数? 那MSDN上有个createDirectory(string Path);可知是怎么用的?(老提示找不到)
CreateDirectory(test,NULL);
CString str="C:\\test";一楼抢先一步
{
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 fFile = FindFirstFile(dd,&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(); for(x1 = 0; x1 < dd.GetLength(); x1++ ) // Parse the supplied CString Directory String
{
if(dd.GetAt(x1) != '\\') // if the Charachter is not a \
tem += dd.GetAt(x1); // add the character to the Temp String
else
{
m_arr.Add(tem); // if the Character is a \
tem += "\\"; // Now add the \ to the temp string
}
if(x1 == dd.GetLength()-1) // If we reached the end of the 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
}
// Now lets see if the directory was successfully created
fFile = FindFirstFile(dd,&fileinfo); m_arr.RemoveAll();
if(fileinfo.dwFileAttributes == FILE_ATTRIBUTE_DIRECTORY)
{
// Directory Exists close file and return
FindClose(fFile);
return TRUE;
}
else
{
// For Some reason the Function Failed Return FALSE
FindClose(fFile);
return FALSE;
}
}
CreateDirectory是API函数?
那MSDN上有个createDirectory(string Path);可知是怎么用的?(老提示找不到)
int main(int argc, char** argv)
{
SECURITY_ATTRIBUTES sa;
SECURITY_DESCRIPTOR sd;
BYTE aclBuffer[1024];
PACL pacl=(PACL)&aclBuffer;
BYTE sidBuffer[100];
PSID psid=(PSID) &sidBuffer;
DWORD sidBufferSize = 100;
char domainBuffer[80];
DWORD domainBufferSize = 80;
SID_NAME_USE snu;
HANDLE file;
InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION);
// SetSecurityDescriptorControl(&sd, SE_DACL_AUTO_INHERITED, 1);
InitializeAcl(pacl, 1024, ACL_REVISION); LookupAccountName(0, "administrator", psid,&sidBufferSize, domainBuffer, &domainBufferSize, &snu);
AddAccessAllowedAce(pacl, ACL_REVISION, GENERIC_ALL , psid);
AddAccessDeniedAce(pacl, ACL_REVISION, FILE_WRITE_DATA |
FILE_WRITE_ATTRIBUTES |
FILE_WRITE_EA |
FILE_APPEND_DATA | DELETE , psid);
SetSecurityDescriptorDacl(&sd, TRUE, pacl, FALSE);
sa.nLength = sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle = FALSE;
sa.lpSecurityDescriptor = &sd; CreateDirectory("c:\\testdir",&sa);
//CloseHandle(file);
}