2011-11-02 18 views
2

顯然,我的桌面不是隻讀的,但stat()和findfirst()函數報告這是不可寫的。我應該使用其他功能嗎?爲什麼?C stat()函數報告Windows桌面文件夾是隻讀的

#include <iostream> 
#include <ShlObj.h> 
#include <sys/stat.h> 

int main() { 
    PWSTR ppszPath; 
    if (::SHGetKnownFolderPath(FOLDERID_Desktop, 0, NULL, &ppszPath)==S_OK) { 
     std::wcout << L"Desktop folder: " << ppszPath << L"\n"; 
     struct _stat64 buf; 
     if (_wstat64(ppszPath, &buf)==0) { 
      std::wcout << L"Writable: " << ((buf.st_mode & _S_IWRITE) != 0? "yes": "no") << L"\n"; 
     } 
    } 
} 

這打印出的Windows7上64:

Desktop folder: C:\Users\heldepn\Desktop 
Writable: no 

回答

3

用於目錄的 「只讀」 標誌是裝飾性和does not control whether the directory contents can be modified。在目錄中創建文件由FILE_ADD_FILE控制,刪除目錄中的文件由FILE_DELETE_CHILD控制,創建子目錄由FILE_ADD_SUBDIRECTORY控制。

+0

謝謝,我希望能夠在stat()的文檔中找到這個信息,因爲這偏離了相應的Posix行爲,但顯然這太需要了。 – Paavo

+1

Windows安全模型比Posix更復雜,因此任何映射都不完整。 'stat'函數是非常懶惰的,只是使用文件屬性。 –

+0

一個小的澄清:只讀標誌不完全是裝飾性的,它確實會阻止目錄被刪除。 –

相關問題