登录服务器的代码:
function ConnectToHost(const user:string;const password:string;const Host:string):boolean;
var
netresource:Tnetresource;
re:dword;
begin
netresource.dwScope:=RESOURCE_GLOBALNET;
netresource.dwUsage:=RESOURCEUSAGE_CONNECTABLE;
netresource.dwType:=RESOURCETYPE_DISK;
netresource.dwDisplayType:=RESOURCEDISPLAYTYPE_SERVER;
netresource.lpLocalName:='';
netresource.lpRemoteName:=pchar('//'+Host); //远程主机名称。
netresource.lpProvider:='';
re:=WNetAddConnection3(
0,
netresource,
pchar(password),//密码
pchar(user), //用户名称
CONNECT_UPDATE_PROFILE
);
result:= re = NO_ERROR;
end;
//打开文件的代码
uses shellApi
//========
var
path :string;
begin
if not ConnectToHost('user','pass','Host') then exit;
path:='//host/共享文件夹/共享文件.TXT';
shellexecute(handle,'open',pchar(path),'','',sw_show);//打开文件
end;
其他要注意的:
1.共享的文件必须是可写的(如果要编辑);
2.必须先登录再打开。