如何在Delphi中获取网络共享的本地名称?
我有一个文件路径到网络共享Z:\someFolder\someFile
的本地别名,并且能够扩展UNC路径\\server\sharename\someFolder\someFile
。但是,我需要远程位置F:\sharedFolder\someFolder\someFile
提前谢谢
答案 0 :(得分:4)
这是一个小例子:
program test;
{$APPTYPE CONSOLE}
{$R *.res}
uses
Windows,
SysUtils;
const
netapi = 'netapi32.dll';
NERR_Success = 0;
STYPE_DISKTREE = 0;
STYPE_PRINTQ = 1;
STYPE_DEVICE = 2;
STYPE_IPC = 3;
STYPE_TEMPORARY = $40000000;
STYPE_SPECIAL = $80000000;
function NetApiBufferFree(Buffer: Pointer): DWORD; stdcall; external netapi;
function NetShareGetInfo(servername, netname: PWideChar; level: DWORD; out bufptr: Pointer): DWORD; stdcall;
external netapi;
type
PShareInfo2 = ^TShareInfo2;
TShareInfo2 = record
shi2_netname: PWideChar;
shi2_type: DWORD;
shi2_remark: PWideChar;
shi2_permissions: DWORD;
shi2_max_uses: DWORD;
shi2_current_uses: DWORD;
shi2_path: PWideChar;
shi2_passwd: PWideChar;
end;
function ShareNameToServerLocalPath(const ServerName, ShareName: string): string;
var
ErrorCode: DWORD;
Buffer: Pointer;
begin
Result := '';
ErrorCode := NetShareGetInfo(PWideChar(ServerName), PWideChar(ShareName), 2, Buffer);
try
if ErrorCode = NERR_Success then
Result := PShareInfo2(Buffer)^.shi2_path;
finally
NetApiBufferFree(Buffer);
end;
end;
procedure Main;
begin
Writeln(ShareNameToServerLocalPath('\\MyServer', 'MyShare'));
end;
begin
try
Main;
except
on E: Exception do
begin
ExitCode := 1;
Writeln(Format('[%s] %s', [E.ClassName, E.Message]));
end;
end;
end.
答案 1 :(得分:2)
这是不可能的,我害怕。
每当您通过网络共享某个文件夹时,您只能获取其名称,文件结构和子文件夹的结构。但是你无法获得有关其父文件夹的任何信息,也绝对不能获得该文件夹所在的硬盘的整个目录结构。
实际上,共享文件夹可能甚至不存在于共享它的计算机上。它可以是某些其他计算机共享的其他共享网络驱动器的子文件夹。