【发布时间】:2013-11-15 06:56:10
【问题描述】:
我需要知道指定目录(本地或具有登录凭据的共享路径)是否具有写入权限。
我正在使用 GetFileAttributes,但它总是返回 FILE_ATTRIBUTE_DIRECTORY 而没有别的。
我的代码如下所示
if(storageLocation != "")
{
//! check if local storage - user name password would be empty
if(storageUsername == "" && storagePassword == "")
{
//! local storage
//! lets check whether the local path is a valid path or not
boost::filesystem::path fpath(storageUsername.c_str());
if(boost::filesystem::exists(fpath))
{
DWORD attrib = ::GetFileAttributes(storageLocation.c_str());
if((attrib != INVALID_FILE_ATTRIBUTES) &&
((attrib & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY))
{
string strWritePermission = "TRUE";
}
}
}
else
{
uncLocation_t uncLocation;
uncLocation.m_location = storageLocation;
uncLocation.m_username = storageUsername;
uncLocation.m_password = storagePassword;
if(0 == connectToUNCLocation(uncLocation)) // My function to connect to UNC location
{
//! successful connection
DWORD attrib = ::GetFileAttributes(storageLocation.c_str());
if((attrib != INVALID_FILE_ATTRIBUTES) &&
((attrib & FILE_ATTRIBUTE_READONLY) != FILE_ATTRIBUTE_READONLY))
{
string strWritePermission = "TRUE";
}
}
}
}
我不明白为什么,但 GetFileAttributes 总是返回 0x16。
我已经通过创建一个共享文件夹并在其中创建 2 个文件夹对其进行了测试。一个具有只读权限,另一个具有默认权限。但在所有 3 种情况下(共享文件夹、只读文件夹和默认权限文件夹)我都得到相同的返回值。
有办法找到写权限,创建一个临时文件(在 GENERIC_WRITE 模式下使用 CreateFile),如果成功创建,删除它。但我不想使用这种方法,因为我不希望我的应用程序在每次用户指定位置时都创建一个临时文件。
请建议应该做什么。
【问题讨论】:
标签: c++ winapi visual-c++ msdn