Question

I am a new user the P4api in c#. I want to open a file for editing in Perforce through C#.

How can I access the "depot" in Perforce?
How can I choose the a file and open it for edit?
How shall the procedure be realized in c#?

That it is the code for the connection with Perforce Server

public void Connection()
{
    Repository rep = null;
    Server server = null;
    try
    {
      // ** Initialise the connection variable **//
      string uri = "perforcep4:1666";
      string user = "9955";
      string ws_client = "9955_7111";

      // ** Define the server, repository and connection **//
      server = new Server(new ServerAddress(uri));
      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);
    }
    catch (Exception ex)
    {
        rep = null;
        MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
}

Now this is the function I have writted to open a file for editing in Perforce.

public void CheckOutFile()
{
    connection();

    DepotPath path = new DepotPath("//depot/main/src/...");
    P4Command cmd = new P4Command(rep, "edit", true, String.Format("{0}/...", path));
    P4CommandResult result = cmd.Run();
}

This function calls the function "connection" to create a connection with perforce server. But I don't know how can I search a file in the depot? My function opens all files in depot for edit and that is not my wish.

Was it helpful?

Solution

I assume that you not run this code from the server. In order to change file you need to do the folowing steps.

  1. Sync your workspace (using p4v you will get the command).

  2. Create changelist

    //creation of new changelist 
    public Changelist CreateNewChangelistInWorkspace(string workspace_name, string change_description)
    {
        Repository rep = P4Core.Instance.GetRepository(workspace_name);
        Client client = rep.GetClient(workspace_name);
        client.Host = string.Empty;
        rep.UpdateClient(client);
    
        //creating changelist
        Changelist cl = new Changelist();
        cl.Description = change_description;
        cl.ClientId = workspace_name;
        cl = rep.CreateChangelist(cl);
        return cl;
    }
    
  3. edit your files - The files locate in your computer now so you not need perforce for edit.

  4. reconcile (p4v will give you the command) (and will attach the files to the change list created in step 2).

  5. submit the changelist (changelist.submit()) + repository.updatechangelist(changelist)).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top