我正在嘗試使用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);
}
爲什麼你需要使用如此多的字符串類型:std :: string,System :: String,Wide-char字符串,char-strings? – Ajay 2012-07-07 06:26:10
因此,使用.NET框架檢索列表! – Ajay 2012-07-07 11:31:12
@Ajay 我需要使用System :: String beacuse我正在使用System :: Windows :: Forms和std :: string我需要,因爲一些函數不會使用System :: string。 但是當涉及到寬字符串和char字符串 和'WideCharToMultiByte'函數時,我將該部分從ffd.cFileName轉換爲我可以使用的字符串。 所以如果有更好的方法來做到這一點,我會很樂意嘗試。 – Tejpbit 2012-07-07 11:31:45