2014-10-20 17 views
2
#include "stdafx.h" 
#include <Windows.h> 
#include <conio.h> 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    DWORD d = GetFileAttributes(argv[0]); 
    _TCHAR* temp; 
    printf("%d\n", d); 
    switch(d) 
    { 
     case 2048: temp = L"Compressed"; break; 
     case 32: temp = L"Archive"; break; 
     case 16: temp = L"Directory"; break; 
     case 16384: temp = L"Encrypted"; break; 
     case 2: temp = L"Hidden"; break; 
     case 128: temp = L"Normal"; break; 
     case 1: temp = L"Readonly"; break; 
     case 4: temp = L"System"; break; 
     case 256: temp = L"Temporary"; break; 
     default: temp = L"Error or unsupported attribute"; break; 
    } 
    _tprintf(temp); 
    getch(); 

    return 0; 
} 

這段代碼有什麼問題?即使我在沒有屬性的情況下啓動它,我總是會得到32位的d? 我正在使用visual studio 2010. 謝謝!GetFileAttributes函數

+1

作爲一個方面說明,這些值是相加的。換句話說,它可以被壓縮和隱藏,那麼值就是2048 + 2.在這種情況下,你的代碼不會顯示「on」的兩個狀態,而是通過「default」打印和「錯誤」 。 – crashmstr 2014-10-20 18:09:18

+0

還有什麼我可以提供回答這個問題嗎? – Jeff 2017-04-12 19:44:50

回答

6

argv [0]是您的可執行程序的名稱。只需將索引設置爲1(確保它存在)。您可能還想要使用按位AND操作來確定是否設置了標誌。