[This is preliminary documentation and is subject to change.]

Using P4API.NET

Client programs interact with the Perforce server by:

  1. Initializing a connection.

  2. Sending commands.

  3. Closing the connection.

Initializing a connection

To connect to the Perforce server, your client application must call the Connect() method; for example:

CopyC#
// initialize the connection variables
// note: this is a connection without using a password
string uri = "localhost:6666";
string user = "admin";
string ws_client = "admin_space";


// define the server, repository and connection
Server server = new Server(new ServerAddress(uri));
Repository rep = new Repository(server);
Connection con = rep.Connection;


// use the connection varaibles for this connection
con.UserName = user;
con.Client = new Client();
con.Client.Name = ws_client;


// connect to the server
con.Connect(null);
Sending commands

To send commands to the Perforce server, your client can use P4 API.NET methods; for example:

CopyC#
// set the options for the p4 changes command
string clientName = "admin_space";
int maxItems = 5;
string userName = "admin";
Options opts = new Options(ChangesCmdFlags.LongDescription,
        clientName, maxItems, ChangeListStatus.None, userName);


// run the command against the current repository
IList<Changelist> changes = rep.getChangelists(opts);


// changes will contain the data returned by the command
// p4 changes -L -c admin_space -m 5 -u admin
Closing the connection

To disconnect from the Perforce server, your client application must call the Disconnect() method; for example:

CopyC#
// disconnect from the server
con.Disconnect(null);
Running commands directly

To run commands that do not have methods in the .NET API, your client can use the P4Command.Run() method; for example:

CopyC#
// create a new command using parameters:
// repository, command, tagged, arguments
cmd = new P4Command((rep, "changes", true, null);
Options opts = new Options();
opts["-m"] = 50;


//run command and get results
P4CommandResult results = cmd.Run(opts);


// results will contain the data returned by the command
// p4 -ztag changes -m 50