非常感谢大家的关注!!!不过还是期盼着高手的到来!!! (200分)

H

HD_Copy

Unregistered / Unconfirmed
GUEST, unregistred user!
在Windows资源管理器右边的文件列表中,所显示的每个文件都有“类型”这一项,如果是系统能够识别的
文件,如:CONFIG.SYS,则类型中显示“系统文件”;如果是系统不可识别的文件,如:CONFIG.XYZ,则
类型中显示“XYZ文件”。

我想通过程序将一个目录下的所有文件的“文件类型”在ListBox控件中列出来,我是这样做的,新建一
工程,在Form上分别放置ListBox1、Button1、Button2这三个控件,下面分别是Delphi和C++Builder的
实现代码:(代码很少,也很简单)

Delphi代码:
//========================================================================================
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls ,ShellAPI , FileCtrl;

type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
Button2: TButton;
procedure Button2Click(Sender: TObject);
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
procedure SearchFiles(CurrentPath : AnsiString);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}
procedure TForm1.SearchFiles(CurrentPath : AnsiString);
var
sr : TSearchRec;
FileInfo : TSHFileInfo;
iFound : Integer;
begin
iFound := FindFirst(CurrentPath + '*.*', faAnyFile, sr);
while iFound=0 do
begin
if (sr.Attr<>faDirectory)and(sr.Name<>'.')and(sr.Name<>'..') then
begin
SHGetFileInfo(PChar(CurrentPath+sr.Name), 0, FileInfo, SizeOf(FileInfo), SHGFI_TYPENAME);
ListBox1.Items.Add(FileInfo.szTypeName);
end;
iFound := FindNext(sr);
end;
FindClose(sr);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.Items.Clear;
SearchFiles('C:/Winnt/system32/');
end;

procedure TForm1.Button1Click(Sender: TObject);
var
SelectPath : AnsiString;
begin
SelectPath := '';
if SelectDirectory('请您选择分类库所对应的路径:', '', SelectPath) then
begin
if Length(SelectPath) <> 3 then
SelectPath := SelectPath + '/';
ListBox1.Items.Clear;
SearchFiles(SelectPath);
end;
end;

end.
//========================================================================================

C++Builder代码:
//========================================================================================
//头文件
//---------------------------------------------------------------------------

#ifndef Unit1H
#define Unit1H
//---------------------------------------------------------------------------
#include <Classes.hpp>
#include <Controls.hpp>
#include <StdCtrls.hpp>
#include <Forms.hpp>
#include <FileCtrl.hpp>
//---------------------------------------------------------------------------
class TForm1 : public TForm
{
__published: // IDE-managed Components
TListBox *ListBox1;
TButton *Button1;
TButton *Button2;
void __fastcall Button1Click(TObject *Sender);
void __fastcall Button2Click(TObject *Sender);
private: // User declarations
void __fastcall SearchFiles(AnsiString CurrentPath);
public: // User declarations
__fastcall TForm1(TComponent* Owner);
};
//---------------------------------------------------------------------------
extern PACKAGE TForm1 *Form1;
//---------------------------------------------------------------------------
#endif
//========================================================================================

//单元文件

//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "Unit1.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------

void __fastcall TForm1::Button1Click(TObject *Sender)
{
AnsiString SelectPath = "";
if(SelectDirectory("请您选择分类库所对应的路径:", "", SelectPath))
{
if( SelectPath.Length() != 3 ) //靠,居然有这样的判断,晕!
SelectPath = SelectPath + "//";
ListBox1->Items->Clear();
SearchFiles(SelectPath);
}
}
//---------------------------------------------------------------------------
void __fastcall TForm1::SearchFiles(AnsiString CurrentPath)
{
TSearchRec sr;
TSHFileInfo FileInfo;
int iFound;
iFound = FindFirst(CurrentPath + "*.*", faAnyFile, sr);
while( iFound == 0 )
{
if( (sr.Name != ".") && (sr.Name != "..") && ((sr.Attr & faDirectory) != faDirectory) )
{
SHGetFileInfo((CurrentPath+sr.Name).c_str(), 0, &FileInfo, sizeof(FileInfo), SHGFI_TYPENAME);
ListBox1->Items->Add(FileInfo.szTypeName);
}
iFound = FindNext(sr);
}
FindClose(sr);
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
AnsiString SelectPath = "C://WINNT//system32//";
ListBox1->Items->Clear();
SearchFiles(SelectPath);
}
//---------------------------------------------------------------------------

//========================================================================================

好,下面怪问题就来了,运行程序,如果你先点击Button1,也就是通过选择路径的方式,没有任何问题,
接下来,不论你在单击Button1还是Button2,都没问题(我说的没问题是指能在ListBox中正确显示包
括那些不可识别的文件在内的所有文件的文件类型)
但是反过来,在程序刚运行后,你先单击了Button2,也就是通过直接传递路径参数的方式,那些不可
识别的文件的文件类型就显示不出来了,然后,你无论是再按那个按钮,都不能正确的显示了
再说得明白一点,就是在程序运行后,你先单击Button1,就没问题;先单击Button2,不可识别的文件
类型就显示成空,这是多么奇怪的事啊!!哪位大侠帮我解释一下原因,并给出解决方法(必有重谢!!)
 
帮不上你!
up.........
 
报告老大:
没有发现什么情况!
 
问题出在
SelectDirectory('请您选择分类库所对应的路径:', '', SelectPath);
 
为了便于熟悉Delphi和C++Builder的朋友阅读,分别给出了实现代码,而且,我上面的代码很完整,
你新建一工程,放上ListBox1,Button1,Button2,然后将代码全部拷贝过去,就能运行,
大家帮我看看,在你们的机器上有没有我说的情况,谢谢大家!!
 
老大,我这里没有问题呀?
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls,shellapi,FileCtrl;

type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
ListBox1: TListBox;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
procedure SearchFiles(CurrentPath : AnsiString);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.SearchFiles(CurrentPath : AnsiString);
var
sr : TSearchRec;
FileInfo : TSHFileInfo;
iFound : Integer;
begin
iFound := FindFirst(CurrentPath + '*.*', faAnyFile, sr);
while iFound=0 do
begin
if (sr.Attr<>faDirectory)and(sr.Name<>'.')and(sr.Name<>'..') then
begin
SHGetFileInfo(PChar(CurrentPath+sr.Name), 0, FileInfo, SizeOf(FileInfo), SHGFI_TYPENAME);
ListBox1.Items.Add(FileInfo.szTypeName);
end;
iFound := FindNext(sr);
end;
FindClose(sr);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
SelectPath : AnsiString;
begin
SelectPath := '';
if SelectDirectory('请您选择分类库所对应的路径:', '', SelectPath) then
begin
if Length(SelectPath) <> 3 then
SelectPath := SelectPath + '/';
ListBox1.Items.Clear;
SearchFiles(SelectPath);
end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
ListBox1.Items.Clear;
SearchFiles('C:/Winnt/system32/');
end;

end.
 
win2000 专业版,delphi6.0,delphi和win2000都没打补订
无论怎样单击,都可以正确显示呀!顺续也没有关系!
 
我也没问题
 
看来是你的系统出了点毛病。。。。。。。。。。。[:)]
 
我这里有问题,不知道怎么回事!
先点button2时,SHGetFileInfo函数返回的szTypeName的第一个字符不正确。
但是先点button1时,SHGetFileInfo函数返回的szTypeName却是对的。
不知道什么??真的好奇怪??
 
估计是您的系统由问题了,不幸,重装吧!!![^]
 
没问题。
 
老大,我的也没问题.报告完毕.
 
呵呵,可以准确确定文件类型!:)其他的我帮不上忙了!
function MrsGetFileType(const strFilename: string): string;
var
FileInfo: TSHFileInfo;
begin
FillChar(FileInfo, SizeOf(FileInfo), #0);
SHGetFileInfo(PChar(strFilename), 0, FileInfo, SizeOf(FileInfo), SHGFI_TYPENAME);
Result := FileInfo.szTypeName;
end;


 
to HD-COPY
你的代码关键少了:FillChar(FileInfo, SizeOf(FileInfo), #0); !!!
具我个人愚见,呵呵!
如果满意,分分拿来哟:)我现在好穷的啦,都快讨米了!!!:PP
 
to hd-copy
qq:???????
 
我不知道怎样给你发消息,在大富翁中
 
没问题啊
 
我还没看你的代码,
不过对这个问题,我是自己访问注册表实现的,这样最稳定。
 
顶部