2012-07-06 114 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无需任何投射就可以编译。