如何列出远程计算机文件夹的内容

时间:2011-07-20 17:48:32

标签: windows delphi winapi networking delphi-xe

我正在寻找一个Windows api功能或其他方式来获取位于我LAN上的计算机中的文件夹的内容(文件夹和文件)。当然,我有一个有效的Windows用户和密码,我想访问的每台机器。

3 个答案:

答案 0 :(得分:6)

您可以使用WMI,查看CIM_DataFileCIM_Directory类。

一些笔记

1.首先,您必须在客户端计算机中启用wmi远程访问。阅读这些文章,看看这个以及Windows版本Connecting to WMI on a Remote ComputerSecuring a Remote WMI Connection之间的差异。

2.始终必须使用过滤器(Where条件)来限制这些WMI类的结果。

3.总是必须使用Drive字段作为条件,因为这些类会返回所有驱动器的文件。

4.Wmi将\(反斜杠)字符解释为保留符号,因此您必须转义该字符以避免WQL语句出现问题。

德尔福代码

{$APPTYPE CONSOLE}

uses
  SysUtils,
  ActiveX,
  ComObj,
  Variants;

procedure  GetRemoteFolderContent(Const WbemComputer,WbemUser,WbemPassword,Path:string);
const
  wbemFlagForwardOnly = $00000020;
var
  FSWbemLocator : OLEVariant;
  FWMIService   : OLEVariant;
  FWbemObjectSet: OLEVariant;
  FWbemObject   : OLEVariant;
  oEnum         : IEnumvariant;
  iValue        : LongWord;    
  WmiPath       : string;
  Drive         : string;
begin;
  //The path  
  //Get the drive 
  Drive   :=ExtractFileDrive(Path);
  //get the path and add a backslash to the end
  WmiPath :=IncludeTrailingPathDelimiter(Copy(Path,3,Length(Path)));
  //escape the backslash character
  WmiPath :=StringReplace(WmiPath,'\','\\',[rfReplaceAll]);

  Writeln('Connecting');
  FSWbemLocator := CreateOleObject('WbemScripting.SWbemLocator');
  //Establish the connection
  FWMIService   := FSWbemLocator.ConnectServer(WbemComputer, 'root\CIMV2', WbemUser, WbemPassword);

  Writeln('Files');
  Writeln('-----');
  //Get the files from the specified folder
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM CIM_DataFile Where Drive="%s" AND Path="%s"',[Drive,WmiPath]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    Writeln(Format('%s',[FWbemObject.Name]));
    FWbemObject:=Unassigned;
  end;

  Writeln('Folders');
  Writeln('-------');
  //Get the folders from the specified folder
  FWbemObjectSet:= FWMIService.ExecQuery(Format('SELECT * FROM CIM_Directory Where Drive="%s" AND Path="%s"',[Drive,WmiPath]),'WQL',wbemFlagForwardOnly);
  oEnum         := IUnknown(FWbemObjectSet._NewEnum) as IEnumVariant;
  while oEnum.Next(1, FWbemObject, iValue) = 0 do
  begin
    Writeln(Format('%s',[FWbemObject.Name]));
    FWbemObject:=Unassigned;
  end;
end;


begin
 try
    CoInitialize(nil);
    try
      GetRemoteFolderContent('remote_machine','user','password','C:\');
      GetRemoteFolderContent('remote_machine','user','password','C:\Program Files');
    finally
      CoUninitialize;
    end;
 except
    on E:EOleException do
        Writeln(Format('EOleException %s %x', [E.Message,E.ErrorCode]));
    on E:Exception do
        Writeln(E.Classname, ':', E.Message);
 end;
 Writeln('Press Enter to exit');
 Readln;
end.

答案 1 :(得分:2)

没有授权部分,它很简单。执行授权的正确方法是调用Windows.pas方法WNetAddConnection2并按此方式执行。

但是,因为我处于一个简单的黑客模式,我试过这个,它基本上有效:

uses Types, IOUtils, ShellApi; // Works in Delphi XE.

procedure TForm5.Button1Click(Sender: TObject);
var
 dirs:TStringDynArray;
 files:TStringDynArray;
 apath, dir,filename:String;
begin
 ListBox1.Items.Clear;
 apath := '\\hostname\sharename';

// This should be calling WNetAddConnection2:
// instead It's an evil (portable) hack.
 ShellExecute(HWND(0), 'open', PChar('net use /delete '+  apath), 
    nil,nil,SW_SHOW );
 ShellExecute(HWND(0), 'open', PChar('net use '+ apath+' /user:uid pswd'),
    nil,nil,SW_SHOW );

  dirs := TDirectory.GetDirectories(apath);
  if Length(dirs)=0 then
      ListBox1.Items.Add('None found.')
  else
  for dir in dirs do
      ListBox1.Items.Add('Directory: '+dir);
  files := TDirectory.GetFiles(apath);
  for filename in files do
      ListBox1.Items.Add('File: '+filename );

end;

为ShellExecute“net use”的丑陋黑客道歉。 (Grin)请注意,我已选择“挂载”此共享文件夹而不给它一个驱动器号,从而避免了如果已映射该驱动器该怎么办的问题。

这是一个带有WNetAddConnection2代码示例的good link,我将链接到该代码而不是偷猎。它显示了一个非邪恶方式的样本。 :-)然后你可以使用我上面显示的目录枚举代码。

答案 2 :(得分:0)

我认为这包含在Warren的回答中,但是为了切入追逐, IOUtils.TDirectory 支持UNC:

implementation
uses IOUtils,types;

procedure GetFiles;
var
  i: integer;
  files: TStringDynArray;
begin
  files := TDirectory.GetFiles('\\aServer\aPath\aShare\', '*.aFileFilter');
  for i := Low(files)to High(files) do
       memo1.Lines.Add(files[i]);
end;

等等...