2010-02-22 14 views
19

का उपयोग कर निर्देशिका निर्देशिका सामग्री सूचीबद्ध करना मैं विंडोज पर सी का उपयोग कर एक संरचना में निर्देशिका की सामग्री को सूचीबद्ध और स्टोर करना चाहता हूं।सी और विंडोज

मैं किसी भी व्यक्ति को जिस कोड को ढूंढ रहा हूं उसे लिखने के लिए जरूरी नहीं है, बल्कि मुझे सही दिशा में इंगित करें जब मुझे लगता है कि कौन सी लाइब्रेरी मुझे देखना चाहिए।

मैं कुछ घंटों के लिए गुगलिंग कर रहा हूं और मुझे लगता है कि सी #, सी ++ समाधान है इसलिए किसी भी मदद की सराहना की जाएगी।

+1

आपका सी ++ समाधान क्या एपीआई तुम बनाने की जरूरत है कॉल तुम्हें दिखाता हूँ। –

उत्तर

38

वैसे ही जैसे हर किसी ने कहा कि (FindFirstFile, FindNextFile और FindClose के साथ) ... लेकिन प्रत्यावर्तन के साथ!

bool ListDirectoryContents(const char *sDir) 
{ 
    WIN32_FIND_DATA fdFile; 
    HANDLE hFind = NULL; 

    char sPath[2048]; 

    //Specify a file mask. *.* = We want everything! 
    sprintf(sPath, "%s\\*.*", sDir); 

    if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) 
    { 
     printf("Path not found: [%s]\n", sDir); 
     return false; 
    } 

    do 
    { 
     //Find first file will always return "." 
     // and ".." as the first two directories. 
     if(strcmp(fdFile.cFileName, ".") != 0 
       && strcmp(fdFile.cFileName, "..") != 0) 
     { 
      //Build up our file path using the passed in 
      // [sDir] and the file/foldername we just found: 
      sprintf(sPath, "%s\\%s", sDir, fdFile.cFileName); 

      //Is the entity a File or Folder? 
      if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) 
      { 
       printf("Directory: %s\n", sPath); 
       ListDirectoryContents(sPath); //Recursion, I love it! 
      } 
      else{ 
       printf("File: %s\n", sPath); 
      } 
     } 
    } 
    while(FindNextFile(hFind, &fdFile)); //Find the next file. 

    FindClose(hFind); //Always, Always, clean things up! 

    return true; 
} 

ListDirectoryContents("C:\\Windows\\"); 

और अब इसके यूनिकोड समकक्ष:

bool ListDirectoryContents(const wchar_t *sDir) 
{ 
    WIN32_FIND_DATA fdFile; 
    HANDLE hFind = NULL; 

    wchar_t sPath[2048]; 

    //Specify a file mask. *.* = We want everything! 
    wsprintf(sPath, L"%s\\*.*", sDir); 

    if((hFind = FindFirstFile(sPath, &fdFile)) == INVALID_HANDLE_VALUE) 
    { 
     wprintf(L"Path not found: [%s]\n", sDir); 
     return false; 
    } 

    do 
    { 
     //Find first file will always return "." 
     // and ".." as the first two directories. 
     if(wcscmp(fdFile.cFileName, L".") != 0 
       && wcscmp(fdFile.cFileName, L"..") != 0) 
     { 
      //Build up our file path using the passed in 
      // [sDir] and the file/foldername we just found: 
      wsprintf(sPath, L"%s\\%s", sDir, fdFile.cFileName); 

      //Is the entity a File or Folder? 
      if(fdFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY) 
      { 
       wprintf(L"Directory: %s\n", sPath); 
       ListDirectoryContents(sPath); //Recursion, I love it! 
      } 
      else{ 
       wprintf(L"File: %s\n", sPath); 
      } 
     } 
    } 
    while(FindNextFile(hFind, &fdFile)); //Find the next file. 

    FindClose(hFind); //Always, Always, clean things up! 

    return true; 
} 

ListDirectoryContents(L"C:\\Windows\\"); 
+1

यह "यूनिकोड" नहीं है-हालांकि मित्रवत। – dreamlax

+3

हाँ, यह एनटीएफएस वैकल्पिक धाराओं को सूचीबद्ध नहीं करता है या कोई बैकअप भी करता है। लेकिन उन्होंने यह नहीं कहा कि वह कोरियाई तस्वीर एल्बम को पार करना चाहता था। किसी भी तरह से, यह सिर्फ एक नमूना है। – NTDLS

+1

ठीक है, मैं कोरियाई तस्वीर एल्बम को पार करता हूं ...: डी – NTDLS

5

फ़ाइल सामग्री सूचीबद्ध करने के लिए आप इन एपीआई के साथ एक निर्देशिका खोज सकते हैं: FindFirstFileEx, FindNextFile और CloseFind। आपको #include <windows.h की आवश्यकता होगी, जो आपको Windows API तक पहुंच प्राप्त करेगी। वे सी कार्य हैं और सी ++ के साथ संगत हैं। यदि आप "विशेष रूप से सी ++" चाहते हैं, तो एमएफसी का उपयोग करके लिस्टिंग निर्देशिकाओं की खोज करने का प्रयास करें।

+0

क्या वह एमएफसी का उपयोग कर रहा है? (यह शैतान है!) – NTDLS

+0

मुझे नहीं पता। मैं या तो प्रशंसक नहीं हूं, लेकिन यह आपको चीजों का ओओ व्यू देता है। –

संबंधित मुद्दे