delphinetwork-programmingdirectoryshared

connect to a password protected shared folder over network using Delphi


I have a multi user Delphi program which needs a shared folder over network to store data. I want the program changes the files in that folder but not normal users (who can see this folder) or network viruses...

I want to protect this folder with a password (windows 7) but I need to write new files or edit existing files via my program and I don't know how to do this.

Briefly I need to connect and disconnect to a shared folder via code like this

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

Is this possible?


Solution

  • Something like this would probably do the trick

    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;