2012-07-06 87 views
2

我正在嘗試使用this example 來創建一個程序,該程序將列出Windows::Forms::ListBox中目錄中的所有文件名。在C++目錄中列出文件

由於用戶不會進行任何輸入,因此我不需要 void DisplayErrorBox(LPTSTR lpszFunction)函數以及其他錯誤檢查。

當我點擊觸發事件的按鈕時,這就是列表框中顯示的內容。

​​

此外,每次單擊按鈕時只出現一行。 它應該找到目錄中的所有文件,並列出它們,而不是每次單擊按鈕時都會找到下一個文件。

我也想用一個相對strPath的,不是絕對的... 到目前爲止,這是我的代碼完成:

private: 
    void List_Files() 
    { 
     std::string strPath = "C:\\Users\\Andre\\Dropbox\\Programmering privat\\Diablo III DPS Calculator\\Debug\\SavedProfiles";  
     TCHAR* Path = (TCHAR*)strPath.c_str(); 

     WIN32_FIND_DATA ffd; 
     LARGE_INTEGER filesize; 
     TCHAR szDir[MAX_PATH]; 
     size_t length_of_arg; 
     HANDLE hFind = INVALID_HANDLE_VALUE; 

     // Prepare string for use with FindFile functions. First, copy the 
     // string to a buffer, then append '\*' to the directory name. 

     StringCchCopy(szDir, MAX_PATH, Path); 
     StringCchCat(szDir, MAX_PATH, TEXT("\\*")); 

     // List all the files in the directory with some info about them. 

     do 
     { 
      if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) 
      { 
       //If it's a directory nothing should happen. Just continue with the next file. 

      } 
      else 
      { 
       //convert from wide char to narrow char array 
       char ch[260]; 
       char DefChar = ' '; 

       WideCharToMultiByte(CP_ACP,0,(ffd.cFileName),-1, ch,260,&DefChar, NULL); 

       //A std:string using the char* constructor. 
       std::string str(ch); 
       String^sysStr = gcnew String(str.c_str()); 

       MessageBox::Show("File Found", "NOTE"); 
       ListBoxSavedFiles->Items->Add (sysStr); 

      } 
     } 
     while (FindNextFile(hFind, &ffd) != 0); 

     FindClose(hFind); 
    } 
+0

爲什麼你需要使用如此多的字符串類型:std :: string,System :: String,Wide-char字符串,char-strings? – Ajay 2012-07-07 06:26:10

+0

因此,使用.NET框架檢索列表! – Ajay 2012-07-07 11:31:12

+0

@Ajay 我需要使用System :: String beacuse我正在使用System :: Windows :: Forms和std :: string我需要,因爲一些函數不會使用System :: string。 但是當涉及到寬字符串和char字符串 和'WideCharToMultiByte'函數時,我將該部分從ffd.cFileName轉換爲我可以使用的字符串。 所以如果有更好的方法來做到這一點,我會很樂意嘗試。 – Tejpbit 2012-07-07 11:31:45

回答

4

FindFirstFile()不會被調用,您需要之前調用它打電話FindNextFile()

HANDLE hFind = FindFirstFile(TEXT("C:\\Users\\Andre\\Dropbox\\Programmering privat\\Diablo III DPS Calculator\\Debug\\SavedProfiles\\*"), &ffd); 

if (INVALID_HANDLE_VALUE != hFind) 
{ 
    do 
    { 
     //... 

    } while(FindNextFile(hFind, &ffd) != 0); 
    FindClose(hFind); 
} 
else 
{ 
    // Report failure. 
} 
0

演員(TCHAR*)strPath.c_str();是錯的。從您使用WideCharToMultiByte我知道(TCHAR*)strPath.c_str();正在將char const*轉換爲wchar_t*。這不僅會丟失const,但寬度也是錯誤的。

+0

雖然你正在識別代碼的問題,那不幫助OP解決問題。 C++中所有不同的字符串類型都可能非常混亂。 – 2012-07-09 16:48:20

1

如果你不介意使用Boost,您可以使用directory_iterator

using boost::filesystem; 

path p("some_dir"); 
for (directory_iterator it(p); it != directory_iterator(); ++it) { 
    cout << it->path() << endl; 
} 

它在Windows也和它肯定看起來簡單得多。當然,您需要稍微修改一下當前的代碼,但從長遠來看,這是值得的。

0

如果您使用的是Visual Studio,則將配置設置更改爲Use Multibyte Character set。這將使你的TCHAR無需任何投射就可以編譯。