微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我正在寻找一个Windows APIfunction或另一种方式来获取我的局域网上的一台计算机上的文件夹的内容文件夹和文件)。 closures当然,我有一个有效的Windows用户密码每台机器,我想访问。

如何检索使用Delphi 7在Windows中的所有磁盘的磁盘签名?

在Delphi 7/2006应用程序的Win8注册自定义协议处理程序

为什么应用程序运行3周后会popup一个错误,“控件没有父窗口”?

为什么在MouseMove事件中调用WindowFromPoint时突出显示窗体的系统button?

alignment文本框

您可以使用WMI,检查CIM_DataFile和CIM_Directory类。

一些笔记

首先你必须在客户端机器上启用wmi远程访问。 阅读这些文章,了解如何做到这一点以及Windows版本之间的差异在Connecting to WMI on a Remote Computer , Securing a Remote WMI Connection 。

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

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

4.Wmi将 (反斜杠)字符解释为一个保留符号,所以您必须避开该字符以避免WQL语句出现问题。

Delphi代码

{$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,'rootCIMV2',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"',[FWbemObject.Name])); FWbemObject:=Unassigned; end; end; begin try CoInitialize(nil); try GetRemoteFolderContent('remote_machine','user','password','C:'); GetRemoteFolderContent('remote_machine','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.

没有授权部分,这很简单。 执行授权的正确方法调用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 := '\hostnamesharename'; // This should be calling WNetAddConnection2: // instead It's an evil (portable) hack. ShellExecute(HWND(0),'open',PChar('net use /delete '+ apath),nil,SW_SHOW ); ShellExecute(HWND(0),PChar('net use '+ apath+' /user:uid pswd'),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的“网络使用”丑陋的黑客道歉。 (Grin)请注意,我已经选择“安装”此共享文件夹而不给它一个驱动器号,避免了如果该驱动器已被映射该怎么办的问题。

这里有一个与WNetAddConnection2代码示例的好链接 ,我将链接到而不是偷猎。 它显示一个非邪恶的方式来做到这一点。 :-)然后,您可以使用目录枚举代码,如上所示。

我想这是包含在沃伦的答案中,但要切入题, IoUtils.Tirectory支持UNCs:

implementation uses IoUtils,types; procedure GetFiles; var i: integer; files: TStringDynArray; begin files := TDirectory.GetFiles('\aserveraPathaShare','*.aFileFilter'); for i := Low(files)to High(files) do memo1.Lines.Add(files[i]); end;

等等…

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐