如何获得Delphi中网络共享的本地名称?

我有一个指向网络共享Z:\someFolder\someFile的本地别名的Filepath,并且能够扩展UNC路径\\server\sharename\someFolder\someFile。但是,我需要远程位置F:\sharedFolder\someFolder\someFile上的本地路径

先感谢您

最佳答案

这是一个小例子:

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.

关于delphi - 获取Delphi中网络共享的本地名称,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24302700/

10-12 01:39