以下是我在msdn上查到的关于CreateDirectory()的用法的程序示例。但我的编译器提示有错……。我编译环境为:vc6.0,控制台程序。
不知道在这种环境下我应该如何完成以下功能呢?程序功能要求:首先检测路径为"../abc/"的文件夹是否存在,存在就向其中创建一个文件;如果不存在,就创建这样一样新文件夹。
用CreateDirectory()怎么来创建一个新文件夹呢?我试过了,如果路径为"../abc/"的文件夹不存在,用CreateDirectory("../abc/", NULL);这个语句也不会来创建这样一个文件夹。这是为什么呢?要怎么使用这个函数才能创建一个新的文件夹呢?
using namespace System;
using namespace System::IO;
int main()
{
// Specify the directory you want to manipulate.
String^ path = "c:\\MyDir";
try
{
// Try to create the directory.
DirectoryInfo^ di = Directory::CreateDirectory( path );
Console::WriteLine( "The directory was created successfully at {0}.", Directory::GetCreationTime( path ) );
// Delete the directory.
di->Delete();
Console::WriteLine( "The directory was deleted successfully." );
}
catch ( Exception^ e )
{
Console::WriteLine( "The process failed: {0}", e );
}
// 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 : 1
The file requested "C:\TEST\file.doc" is not a valid file
The return from function is : 0
pszPath
[in] Pointer to a null-terminated string of maximum length MAX_PATH that contains the full path of the object to verify.
Return Value
Returns TRUE if the file exists, or FALSE otherwise. Call GetLastError for extended error information.
Remarks
This function tests the validity of the path. It works only on the local file system or on a remote drive that has been mounted to a drive letter. It returns FALSE if a mounted remote drive is out of service.