P4Connection p4 = new P4Connection();
p4.Connect();
P4PendingChangelist cl = p4.CreatePendingChangelist("My New Changelist\nVery, Very bad description!\nShame on me!");
p4.Run("edit", "-c", cl.Number.ToString(), "//depot/path/foo.cs", "//depot/path/bar.cs");
// Do something to manipulate the files
cl.Submit();
p4.Disconnect();
The P4Connection class is the main player in P4.Net. This represents a connection to the Perforce server. Every utility that uses P4.Net will have some variation of the following code:
P4Connection p4 = new P4Connection();
p4.Connect();
// Run some commands
p4.Disconnect();
Rule number 1: Always remember to disconnect. This frees unmanaged memory, and cleanly disconnects from the Perforce server. P4Connection implements IDisposable, and the Dispose and Disconnect methods can be used interchangeably.
P4.Net is based off the command-line syntax (as are most other Perforce APIs). Almost all of the commands you issue in P4.Net will use the same arguments as the p4 client executable. For example, say you need to find the latest submitted changelist under a given path (//depot/path).
c:\> p4 changes -m1 -s submitted //depot/path/...
P4Connect p4 = new P4Connection();
p4.Connect();
P4Recordset changes = p4.Run("changes", "-m1", "-s", "submitted", "//depot/path/...");
p4.Disconnect();
If you don’t know what all the arguments for p4 changes mean, then
The following is a list of common form commands:
When fetching or saving a form, do not use the '-o' and '-i' flags. P4.Net will automatically include them.
The following is a list of common form commands:
When fetching or saving a form, do not use the '-o' and '-i' flags. P4.Net will automatically include them.
The following is a list of common form commands:
When fetching or saving a form, do not use the '-o' and '-i' flags. P4.Net will automatically include them.
The following is a list of common form commands:
When fetching or saving a form, do not use the '-o' and '-i' flags. P4.Net will automatically include them.
p4 print command line.
p4 print command line.p4 print command line.p4 print command.
public class PasswordSetter
{
private P4Connection _p4 = null;
private string _oldPassword = "";
private string _newPassword = "";
PasswordSetter(P4Connection p4)
{
_p4 = p4;
}
public void SetPassword(string OldPassword, string NewPassword)
{
OnPromptEventHandler eh = new OnPromptEventHandler(OnPrompt);
_p4.OnPrompt += eh;
_oldPassword = OldPassword;
_newPassword = NewPassword;
//run passwd
P4UnParsedRecordSet r = _p4.RunUnParsed("passwd");
//Clear the event just in case
_p4.OnPrompt -= eh;
//Clear the passwords from memory
_newPassword = "";
_oldPassword = "";
}
private void OnPrompt(object sender, P4PromptEventArgs e)
{
switch (e.Message)
{
case ("Enter old password: "):
e.Response = _oldPassword;
break;
case ("Enter new password: "):
e.Response = _newPassword;
break;
case ("Re-enter new password: "):
e.Response = _newPassword;
break;
}
}
}
The following is a list of common form commands:
When fetching or saving a form, do not use the '-o' and '-i' flags. P4.Net will automatically include them.
P4Connection p4 = new P4Connection();
p4.Connect();
P4Form MyClient = p4.Fetch_Form("client");
//Change the root and properties
MyClient["Root"] = @"C:\p4\P4.NetClient";
MyClient["Description"] = "Created from P4.Net!";
//Fetch the clientName (for use later in building the view spec).
string clientName = MyClient["Client"];
// Build a new array, that has one more element than the current view
string[] NewView = new string[MyClient.ArrayFields["View"].Length + 1];
// Copy the existing view lines to the new array
MyClient.ArrayFields["View"].CopyTo(NewView,0);
// Set the new view line
NewView[NewView.Length - 1] = string.Format("//depot/newpath/... //{0}/newpath/...", clientName);
MyClient.ArrayFields["View"] = NewView;
// Save the form
P4UnParsedRecordSet MyResult = p4.Save_Form(MyClient);
p4.Disconnect();