我正在尝试使用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