使用Delphi通过网络连接到受密码保护的共享文件夹

时间:2013-12-02 11:48:07

标签: delphi networking network-programming directory shared

我有一个多用户Delphi程序,它需要网络上的共享文件夹来存储数据。我希望程序更改该文件夹中的文件,但不能更改普通用户(可以看到此文件夹)或网络病毒......

我想用密码保护这个文件夹(Windows 7)但是我需要通过我的程序编写新文件或编辑现有文件,我不知道该怎么做。

简单地说,我需要通过像这样的代码连接和断开共享文件夹

ConnectToFolder(\\myServerMachine\mySharedfolder username:me password:myPassword);
disConnectToFolder(\\myServerMachine\mySharedfolder username:me password:myPassword);

这可能吗?

1 个答案:

答案 0 :(得分:5)

像这样的东西可能会成功

function ConnectShare(Drive, RemotePath, UserName, Password : String):Integer;
var
  NRW : TNetResource;
begin
  with NRW do
  begin
    dwType := RESOURCETYPE_ANY;
    if Drive <> '' then
      lpLocalName := PChar(Drive)
    else
      lpLocalName := nil;
    lpRemoteName := PChar(RemotePath);
    lpProvider := '';
  end;
  Result := WNetAddConnection2(NRW, PChar(Password), PChar(UserName), 0);
end;

function DisconnectShare(Drive : String):Integer;
begin
  Result := WNetCancelConnection2(PChar(Drive), 0, false);
end;
相关问题