MFC中使用listctrl控件,如何把文件夹中的所有文件的属性写入其中

时间:2022-09-21 19:11:52
如上,比如我想把D盘里面我的文档文件夹里面的所有的TXT文件的属性,如文件名,大小,类型,修改时间写入到listctrl控件中。有知道的高手能贴个代码,学习一下。最后在listctrl中显示的效果像下面的图一样。
MFC中使用listctrl控件,如何把文件夹中的所有文件的属性写入其中

12 个解决方案

#1



CString strPath = "D\\*.*"
WIN32_FIND_DATA fd;
HANDLE hfind;
hfind=FindFirstFile(strPath,&fd);
if(hfind!=INVALID_HANDLE_VALUE)
{
 do
 {
      if(fd.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY)
      { //文件名
         fd.cFileName;//文件名
         fd.ftLastWriteTime;//最后一次修改时间
         //类型自己判断
        //文件长度自己算
       fd.nFileSizeHigh 文件长度高三十二位
       fd.nFileSizeLow 文件长度低三十二位
       }
       else
       {//文件夹
        同上
       }
 }
 while(FindNextFile(hfind,&fd));
}

#2


如何读写文件属性这个我会,就是怎么把读写到的属性,一行一行添加到listcrl中有点不懂,比如说:我在那个文件夹中添加 几个文件,然后在listcrl中就能显示出这些文件的属性清单,并且一行一行一一对应,这个该咋弄?

#3



// 插入第一行记录
m_lst.InsertItem(0,_T("Defult.xml"));
m_lst.SetItemText(0,1,_T("2014/07/04 20:52"));
m_lst.SetItemText(0,2,_T("XML文件"));
m_lst.SetItemText(0,2,_T("1KB"));


如果是一个列表,则定义一个索引变量nIndex,将上面的0换成nIndex即可,注意列表控件的索引从0开始:

for ( int nIndex = 0; nIndex < nSzie; nIndex++ )

#4


更正一下:

// 插入第一行记录
m_lst.InsertItem(0,_T("Defult.xml"));  // 插入第1列
m_lst.SetItemText(0,1,_T("2014/07/04 20:52"));// 插入第2列
m_lst.SetItemText(0,2,_T("XML文件"));// 插入第3列
m_lst.SetItemText(0,3,_T("1KB"));
// 插入第4列

#5


感谢大家出招,但是按照楼上的说法是只能一行一行加入吧。。。。不是我想要的那种形式
我的意思是点开一个对话框的按钮,然后弹出另一个对话框,那弹出的对话框在listcrl中能显示出D://我的文档,里面的文件的属性。。然后我再在D://我的文档,里面加入几个文件。再点击第一个对话框上的那个按钮,弹出的对话框的listcrl中显示原来的文件属性和后来加入的文件的属性。 MFC中使用listctrl控件,如何把文件夹中的所有文件的属性写入其中

#6


有知道的么。。。。大家支支招哇

#7


引用 5 楼 qq_16267913 的回复:
感谢大家出招,但是按照楼上的说法是只能一行一行加入吧。。。。不是我想要的那种形式
我的意思是点开一个对话框的按钮,然后弹出另一个对话框,那弹出的对话框在listcrl中能显示出D://我的文档,里面的文件的属性。。然后我再在D://我的文档,里面加入几个文件。再点击第一个对话框上的那个按钮,弹出的对话框的listcrl中显示原来的文件属性和后来加入的文件的属性。 MFC中使用listctrl控件,如何把文件夹中的所有文件的属性写入其中

这个需要你获取路径的所有文件信息,然后依次插进去,如果文件已存在,则插入新增的。但是也是要考虑中途有文件删除的问题吧。应该是每次点击时,将原来的都删除掉,然后再将现有的添加进去

#8


自己写的函数:
/************************************************************************/
/* ErgodicFolder 文件夹遍历  (显示文件信息)         */
/************************************************************************/
void  C文件遍历器Dlg::ErgodicFolder(LPCTSTR lpStrPath)
{
CString IsShow;
GetDlgItemText(IDC_EDIT1,IsShow);
if (IsShow == "")
{
MessageBox(_T("路径不能为空!!!"));
return;
}
m_List.DeleteAllItems();//清除内容

CString StrBootPath(lpStrPath);
StrBootPath += _T("\\");
 CFileFind finder;
 CString strExtension;
 GetDlgItemText(IDC_COMBO1,strExtension);
 BOOL bWorking = finder.FindFile(StrBootPath + strExtension);
 /* if (!bWorking)
 {
 MessageBox(_T("没有该类型的文件!!!"));
 return;
 }*/
 while (bWorking)
 {
  bWorking = finder.FindNextFile();
  if (finder.IsDots())
         continue;
  if(finder.IsDirectory()){
  ErgodicFolder(finder.GetFilePath());
  }else{
  CString FileName = finder.GetFileName();//文件名
  ULONGLONG FileLen = finder.GetLength();//文件大小
//  BOOL IsOnly = finder.IsReadOnly();//只读?

  CTime tempTime;
  CString str,str1,str2;
  if (finder.GetCreationTime(tempTime))//创建时间
  {
  str = tempTime.Format(_T("%c"));
  }
  if (finder.GetLastWriteTime(tempTime))//写入时间
  {
  str1= tempTime.Format(_T("%c"));
  }
  if (finder.GetLastAccessTime(tempTime))//访问时间
  {
  str2 = tempTime.Format(_T("%c"));
  }
  //将获得属性显示在listctrl中
 int Index = m_List.InsertItem(0xffff,_T(""));
 m_List.SetItemText(Index,0,FileName);

 CString filelen,IsOk;
 filelen.Format(_T("%I64u"),FileLen);
 m_List.SetItemText(Index,1,filelen);

 IsOk.Format(_T("%c"), finder.IsReadOnly()  ? 'Y' : 'N');
 m_List.SetItemText(Index,2,IsOk);

 m_List.SetItemText(Index,3,str);
 m_List.SetItemText(Index,4,str1);
 m_List.SetItemText(Index,5,str2);
  }
 }
}

#9


/************************************************************************/
/* ErgodicFolder 文件夹遍历  (显示文件信息)         */
/************************************************************************/
void  C文件遍历器Dlg::ErgodicFolder(LPCTSTR lpStrPath)
{
CString IsShow;
GetDlgItemText(IDC_EDIT1,IsShow);
if (IsShow == "")
{
MessageBox(_T("路径不能为空!!!"));
return;
}
m_List.DeleteAllItems();//清除内容

CString StrBootPath(lpStrPath);
StrBootPath += _T("\\");
 CFileFind finder;
 CString strExtension;
 GetDlgItemText(IDC_COMBO1,strExtension);
 BOOL bWorking = finder.FindFile(StrBootPath + strExtension);
 /* if (!bWorking)
 {
 MessageBox(_T("没有该类型的文件!!!"));
 return;
 }*/
 while (bWorking)
 {
  bWorking = finder.FindNextFile();
  if (finder.IsDots())
         continue;
  if(finder.IsDirectory()){
  ErgodicFolder(finder.GetFilePath());
  }else{
  CString FileName = finder.GetFileName();//文件名
  ULONGLONG FileLen = finder.GetLength();//文件大小
//  BOOL IsOnly = finder.IsReadOnly();//只读?

  CTime tempTime;
  CString str,str1,str2;
  if (finder.GetCreationTime(tempTime))//创建时间
  {
  str = tempTime.Format(_T("%c"));
  }
  if (finder.GetLastWriteTime(tempTime))//写入时间
  {
  str1= tempTime.Format(_T("%c"));
  }
  if (finder.GetLastAccessTime(tempTime))//访问时间
  {
  str2 = tempTime.Format(_T("%c"));
  }
  //将获得属性显示在listctrl中
 int Index = m_List.InsertItem(0xffff,_T(""));
 m_List.SetItemText(Index,0,FileName);

 CString filelen,IsOk;
 filelen.Format(_T("%I64u"),FileLen);
 m_List.SetItemText(Index,1,filelen);

 IsOk.Format(_T("%c"), finder.IsReadOnly()  ? 'Y' : 'N');
 m_List.SetItemText(Index,2,IsOk);

 m_List.SetItemText(Index,3,str);
 m_List.SetItemText(Index,4,str1);
 m_List.SetItemText(Index,5,str2);
  }
 }
}

#10


引用
http://blog.csdn.net/lovton/article/details/6527208
看下list control的用法先

#11


现在做出来是这样的,  MFC中使用listctrl控件,如何把文件夹中的所有文件的属性写入其中
这里面头2行出现了文件名“.”,".."但实际我的文件夹中,没有这2个文件。代码如下,请大神帮我分析分析
m_List.InsertColumn(0,_T("名称"));
m_List.InsertColumn(1,_T("大小"));
m_List.InsertColumn(2,_T("时间"));
CRect  rect;
m_List.GetClientRect(&rect);
m_List.SetColumnWidth(0,rect.Width()/3);
m_List.SetColumnWidth(1,rect.Width()/3);
    m_List.SetColumnWidth(2,rect.Width()/3);

CFileFind finder;
BOOL bWorking = finder.FindFile(_T("D:\\22222\\*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
TRACE(_T("%s\n"), (LPCTSTR)finder.GetFileName());
CString FileName = finder.GetFileName();//文件名
int Index = m_List.InsertItem(0xffff,_T(""));
m_List.SetItemText(Index,0,FileName);


     
        ULONGLONG size = finder.GetLength()/1024+1;  //文件大小
// ULONGLONG FileLen = finder.GetLength();   //文件大小
CString filelen;
filelen.Format(_T("%dKB"),size);
m_List.SetItemText(Index,1,(LPCTSTR)filelen);


CTime tempTime;
CString str1;
finder.GetCreationTime(tempTime);//创建时间
str1 = tempTime.Format(_T("%Y/%m/%d %H:%M:%S"));

m_List.SetItemText(Index,2,str1);

#12


 在吗?请教个问题,MFC list control如何获取读取程式当前位置所有文件的文件名 时间 类型 大小 并且时时刷新显示在list control上面,(不要用点击按扭方法获取,要用打开程式自动获取)

#1



CString strPath = "D\\*.*"
WIN32_FIND_DATA fd;
HANDLE hfind;
hfind=FindFirstFile(strPath,&fd);
if(hfind!=INVALID_HANDLE_VALUE)
{
 do
 {
      if(fd.dwFileAttributes!=FILE_ATTRIBUTE_DIRECTORY)
      { //文件名
         fd.cFileName;//文件名
         fd.ftLastWriteTime;//最后一次修改时间
         //类型自己判断
        //文件长度自己算
       fd.nFileSizeHigh 文件长度高三十二位
       fd.nFileSizeLow 文件长度低三十二位
       }
       else
       {//文件夹
        同上
       }
 }
 while(FindNextFile(hfind,&fd));
}

#2


如何读写文件属性这个我会,就是怎么把读写到的属性,一行一行添加到listcrl中有点不懂,比如说:我在那个文件夹中添加 几个文件,然后在listcrl中就能显示出这些文件的属性清单,并且一行一行一一对应,这个该咋弄?

#3



// 插入第一行记录
m_lst.InsertItem(0,_T("Defult.xml"));
m_lst.SetItemText(0,1,_T("2014/07/04 20:52"));
m_lst.SetItemText(0,2,_T("XML文件"));
m_lst.SetItemText(0,2,_T("1KB"));


如果是一个列表,则定义一个索引变量nIndex,将上面的0换成nIndex即可,注意列表控件的索引从0开始:

for ( int nIndex = 0; nIndex < nSzie; nIndex++ )

#4


更正一下:

// 插入第一行记录
m_lst.InsertItem(0,_T("Defult.xml"));  // 插入第1列
m_lst.SetItemText(0,1,_T("2014/07/04 20:52"));// 插入第2列
m_lst.SetItemText(0,2,_T("XML文件"));// 插入第3列
m_lst.SetItemText(0,3,_T("1KB"));
// 插入第4列

#5


感谢大家出招,但是按照楼上的说法是只能一行一行加入吧。。。。不是我想要的那种形式
我的意思是点开一个对话框的按钮,然后弹出另一个对话框,那弹出的对话框在listcrl中能显示出D://我的文档,里面的文件的属性。。然后我再在D://我的文档,里面加入几个文件。再点击第一个对话框上的那个按钮,弹出的对话框的listcrl中显示原来的文件属性和后来加入的文件的属性。 MFC中使用listctrl控件,如何把文件夹中的所有文件的属性写入其中

#6


有知道的么。。。。大家支支招哇

#7


引用 5 楼 qq_16267913 的回复:
感谢大家出招,但是按照楼上的说法是只能一行一行加入吧。。。。不是我想要的那种形式
我的意思是点开一个对话框的按钮,然后弹出另一个对话框,那弹出的对话框在listcrl中能显示出D://我的文档,里面的文件的属性。。然后我再在D://我的文档,里面加入几个文件。再点击第一个对话框上的那个按钮,弹出的对话框的listcrl中显示原来的文件属性和后来加入的文件的属性。 MFC中使用listctrl控件,如何把文件夹中的所有文件的属性写入其中

这个需要你获取路径的所有文件信息,然后依次插进去,如果文件已存在,则插入新增的。但是也是要考虑中途有文件删除的问题吧。应该是每次点击时,将原来的都删除掉,然后再将现有的添加进去

#8


自己写的函数:
/************************************************************************/
/* ErgodicFolder 文件夹遍历  (显示文件信息)         */
/************************************************************************/
void  C文件遍历器Dlg::ErgodicFolder(LPCTSTR lpStrPath)
{
CString IsShow;
GetDlgItemText(IDC_EDIT1,IsShow);
if (IsShow == "")
{
MessageBox(_T("路径不能为空!!!"));
return;
}
m_List.DeleteAllItems();//清除内容

CString StrBootPath(lpStrPath);
StrBootPath += _T("\\");
 CFileFind finder;
 CString strExtension;
 GetDlgItemText(IDC_COMBO1,strExtension);
 BOOL bWorking = finder.FindFile(StrBootPath + strExtension);
 /* if (!bWorking)
 {
 MessageBox(_T("没有该类型的文件!!!"));
 return;
 }*/
 while (bWorking)
 {
  bWorking = finder.FindNextFile();
  if (finder.IsDots())
         continue;
  if(finder.IsDirectory()){
  ErgodicFolder(finder.GetFilePath());
  }else{
  CString FileName = finder.GetFileName();//文件名
  ULONGLONG FileLen = finder.GetLength();//文件大小
//  BOOL IsOnly = finder.IsReadOnly();//只读?

  CTime tempTime;
  CString str,str1,str2;
  if (finder.GetCreationTime(tempTime))//创建时间
  {
  str = tempTime.Format(_T("%c"));
  }
  if (finder.GetLastWriteTime(tempTime))//写入时间
  {
  str1= tempTime.Format(_T("%c"));
  }
  if (finder.GetLastAccessTime(tempTime))//访问时间
  {
  str2 = tempTime.Format(_T("%c"));
  }
  //将获得属性显示在listctrl中
 int Index = m_List.InsertItem(0xffff,_T(""));
 m_List.SetItemText(Index,0,FileName);

 CString filelen,IsOk;
 filelen.Format(_T("%I64u"),FileLen);
 m_List.SetItemText(Index,1,filelen);

 IsOk.Format(_T("%c"), finder.IsReadOnly()  ? 'Y' : 'N');
 m_List.SetItemText(Index,2,IsOk);

 m_List.SetItemText(Index,3,str);
 m_List.SetItemText(Index,4,str1);
 m_List.SetItemText(Index,5,str2);
  }
 }
}

#9


/************************************************************************/
/* ErgodicFolder 文件夹遍历  (显示文件信息)         */
/************************************************************************/
void  C文件遍历器Dlg::ErgodicFolder(LPCTSTR lpStrPath)
{
CString IsShow;
GetDlgItemText(IDC_EDIT1,IsShow);
if (IsShow == "")
{
MessageBox(_T("路径不能为空!!!"));
return;
}
m_List.DeleteAllItems();//清除内容

CString StrBootPath(lpStrPath);
StrBootPath += _T("\\");
 CFileFind finder;
 CString strExtension;
 GetDlgItemText(IDC_COMBO1,strExtension);
 BOOL bWorking = finder.FindFile(StrBootPath + strExtension);
 /* if (!bWorking)
 {
 MessageBox(_T("没有该类型的文件!!!"));
 return;
 }*/
 while (bWorking)
 {
  bWorking = finder.FindNextFile();
  if (finder.IsDots())
         continue;
  if(finder.IsDirectory()){
  ErgodicFolder(finder.GetFilePath());
  }else{
  CString FileName = finder.GetFileName();//文件名
  ULONGLONG FileLen = finder.GetLength();//文件大小
//  BOOL IsOnly = finder.IsReadOnly();//只读?

  CTime tempTime;
  CString str,str1,str2;
  if (finder.GetCreationTime(tempTime))//创建时间
  {
  str = tempTime.Format(_T("%c"));
  }
  if (finder.GetLastWriteTime(tempTime))//写入时间
  {
  str1= tempTime.Format(_T("%c"));
  }
  if (finder.GetLastAccessTime(tempTime))//访问时间
  {
  str2 = tempTime.Format(_T("%c"));
  }
  //将获得属性显示在listctrl中
 int Index = m_List.InsertItem(0xffff,_T(""));
 m_List.SetItemText(Index,0,FileName);

 CString filelen,IsOk;
 filelen.Format(_T("%I64u"),FileLen);
 m_List.SetItemText(Index,1,filelen);

 IsOk.Format(_T("%c"), finder.IsReadOnly()  ? 'Y' : 'N');
 m_List.SetItemText(Index,2,IsOk);

 m_List.SetItemText(Index,3,str);
 m_List.SetItemText(Index,4,str1);
 m_List.SetItemText(Index,5,str2);
  }
 }
}

#10


引用
http://blog.csdn.net/lovton/article/details/6527208
看下list control的用法先

#11


现在做出来是这样的,  MFC中使用listctrl控件,如何把文件夹中的所有文件的属性写入其中
这里面头2行出现了文件名“.”,".."但实际我的文件夹中,没有这2个文件。代码如下,请大神帮我分析分析
m_List.InsertColumn(0,_T("名称"));
m_List.InsertColumn(1,_T("大小"));
m_List.InsertColumn(2,_T("时间"));
CRect  rect;
m_List.GetClientRect(&rect);
m_List.SetColumnWidth(0,rect.Width()/3);
m_List.SetColumnWidth(1,rect.Width()/3);
    m_List.SetColumnWidth(2,rect.Width()/3);

CFileFind finder;
BOOL bWorking = finder.FindFile(_T("D:\\22222\\*.*"));
while (bWorking)
{
bWorking = finder.FindNextFile();
TRACE(_T("%s\n"), (LPCTSTR)finder.GetFileName());
CString FileName = finder.GetFileName();//文件名
int Index = m_List.InsertItem(0xffff,_T(""));
m_List.SetItemText(Index,0,FileName);


     
        ULONGLONG size = finder.GetLength()/1024+1;  //文件大小
// ULONGLONG FileLen = finder.GetLength();   //文件大小
CString filelen;
filelen.Format(_T("%dKB"),size);
m_List.SetItemText(Index,1,(LPCTSTR)filelen);


CTime tempTime;
CString str1;
finder.GetCreationTime(tempTime);//创建时间
str1 = tempTime.Format(_T("%Y/%m/%d %H:%M:%S"));

m_List.SetItemText(Index,2,str1);

#12


 在吗?请教个问题,MFC list control如何获取读取程式当前位置所有文件的文件名 时间 类型 大小 并且时时刷新显示在list control上面,(不要用点击按扭方法获取,要用打开程式自动获取)