Quick start P4 API for .NET on Windows

This quick start guide covers how to:

  • Install P4 API for .NET

  • Connect to the P4 Server

  • Check out a file using P4 API for .NET

  • Commit a file using P4 API for .NET

Before you begin

Ensure you have the following:

  • Ensure you have the following software:

    • Visual Studio (Visual Studio Community is recommended)

    • .NET SDK (Version 6 or newer, .NET 10 LTS is recommended)

      Download Visual Studio Community as this includes .NET.

      To check if .NET is installed, open the command prompt and run dotnet --version. If you see a version number, .NET is installed.

  • Access to a working version of P4 Server.

  • The server and port values for P4 Server.

  • The username for your P4 user.

  • The name of the workspace that contains the files you want to check out, edit, and commit.

  • (Optional) A working version of P4V, to check if a file has been added to a changelist.

Install P4 API for .NET

The easiest way to install P4 API for .NET is using Visual Studio. This involves:

  • Creating a Project

  • Install P4 API for .NET with NuGet

  • Checking P4 API for .NET is installed

Create a Project

  1. Open Visual Studio Community.

  2. Create a new project.

  3. Select Console App and click Next.

  4. In Project name, call the project something like P4APINetQuickstart and click Next.

  5. In Framework, select .NET 8.0 (or newer).

  6. Click Create.

Use this project to install P4 API for .NET using the Solution Explorer

Install P4 API for .NET with NuGet

Perforce publishes P4 API for .NET as an official NuGet package named p4api.net. This is the supported and recommended installation method for .NET projects.

  1. In Visual Studio Community, click view and select Solution Explorer.

  2. In Solution Explorer, right-click Dependencies.

  3. Select Manage NuGet Packages.

  4. Go to the Browse tab and search for p4api.net.

  5. Select p4api.net.

    Author: Perforce Software.

  6. Click Install.

  7. Accept the license prompt to complete the installation.

Visual Studio will then, download the package, add references to your project and resolve dependencies automatically.

P4 API for .NET is now installed on your device and you can see the package in the Solution Explorer.

Verify P4 API for .NET is installed and runs correctly

Before connecting to a P4 Server, verify that P4 API for .NET is installed correctly and available to your project. This step confirms that Visual Studio, .NET, and P4 API for .NET are all working together.

  1. In Solution Explorer, expand Dependencies and look in the Packages folder. You will see a file called p4api.net if P4 API for .NET is installed correctly.

  2. In Solution Explorer, open the Open Program.cs file and replace its contents with the following code:

    Copy
    // Top-level Program.cs
    using Perforce.P4;
    Repository? repo = null;
    Console.WriteLine("P4 API .NET is referenced correctly");
  3. Save this file.

  4. Build the project (Ctrl + Shift + B).

  5. Check the build results. If the build fails with an error such as:

    The type or namespace name 'Perforce' could not be found

    Then the P4 API for .NET package is not installed or not referenced correctly.

  6. Run the project (Ctrl + F5).

    This will open a console window that displays:

    P4 API .NET is referenced correctly

This verification confirms that:

  • The P4 API for .NET NuGet package is installed

  • The project can reference the Perforce.P4 namespace

  • The application builds successfully

  • The application runs successfully

You are now ready to connect to the P4 Server.

Connect to the P4 Server

You need the following credentials to connect to P4 Server.

  • p4.port - The server and port to connect to in the format: server:port.

  • p4.user - A username for your P4 user.

  • p4.client - The name of the workspace you want to connect to.

To find these credential, run:

  • p4 info - Returns the server details and the username of your P4 user.

  • p4 clients -u <p4username> - Returns the workspaces your user has access to. Replace <p4username> with the username of your P4 user.

To connect to P4 Server:

  1. In Solution Explorer, open Program.cs.

  2. Replace all the code in Program.cs with the following script.

    Copy
    using Perforce.P4;

    string uri = "localhost:1666";
    string user = "P4UserName";
    string ws_client = "projects_main";

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

    // use the connection variables for this connection
    con.UserName = user;
    con.Client = new Client { Name = ws_client };

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

    // get server metadata and check version
    // (using null for options parameter)
    ServerMetaData p4info = repo.GetServerMetaData(null);
    ServerVersion version = p4info.Version;
    string release = version.Major;

    Console.WriteLine("Connected to Perforce Server");
    Console.WriteLine($"Server address : {p4info.Address}");
    Console.WriteLine($"Server root    : {p4info.Root}");
    Console.WriteLine($"Server version : {p4info.Version}");
    Console.WriteLine($"Server date    : {p4info.Date}");
  3. Build the script (Ctrl + Shift + B).

  4. Run the script (Ctrl + F5).

  5. If you have successfully connected to P4 Server, you will see the following output:

    Copy
    Connected to Perforce Server
    Server address : localhost:1666
    Server root    : C:\P4ROOT\
    Server version : Perforce.P4.ServerVersion
    Server date    : 13/03/2026 11:53:09

If you receive any errors when connecting to the P4 Server, see Troubleshooting.

Check out a file

The following script will:

  • Connect to P4 Server with your credentials (user and workspace). This is using the code in the previous section.

  • Create a pending changelist with a simple description.

  • Open the target file for edit in that changelist.

  • Return a brief summary of what has been done to the terminal. This includes a changelist number. Make a note of this number is needed when submitting the file.

Update the following credentials in the script:

  • uri - Add the server and port to connect to in the format: server:port

  • user - A username for your P4 user.

  • ws_client - The name of the workspace you want to connect to.

  • depotPath - Filepath to the file in the depot to edit. For example:

    //DemoDepot/pirate_weather_main/PW_OpenAPI.yaml

To check out a file to edit using P4 API for .NET:

  1. In Solution Explorer, open Program.cs.

  2. Replace all the code in Program.cs with the following script.

    Copy
    using Perforce.P4;

    string uri = "localhost:1666";
    string user = "P4UserName";
    string ws_client = "projects_main";

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

    // use the connection variables for this connection
    con.UserName = user;
    con.Client = new Client { Name = ws_client };

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

    // create a numbered changelist
    Changelist changelist = new Changelist();
    changelist.Description = "Edit OpenAPI specification for Pirate Weather";

    changelist = repo.CreateChangelist(changelist);

    Console.WriteLine($"Created changelist {changelist.Id}");

    // open a file for edit

    string depotPath = "//DemoDepot/pirate_weather_main/PW_OpenAPI.yaml";

    FileSpec file = new FileSpec(
        new DepotPath(depotPath)
    );

    EditCmdOptions editOptions = new EditCmdOptions(
        EditFilesCmdFlags.None,
        changelist.Id,
        null
    );

    con.Client.EditFiles(editOptions, file);

    Console.WriteLine(
        $"File opened for edit in changelist {changelist.Id}"
    );
  3. Build the script (Ctrl + Shift + B).

  4. Run the script (Ctrl + F5).

  5. In the terminal, view the changelist number. This means the file has been successfully checked out and added to a changelist. You will need this changelist number in the next section to submit the file.

  6. (Optional) Open P4V to check the correct file has been checked out and the changelist numbers match up.

Commit a file

After you have checked out a file and edited it locally, you must submit the changelist to check the file back in to the depot.

The following script will:

  • Connect to P4 Server with your credentials (user and workspace). This is using the code in the previous section.

  • Submit an existing numbered changelist.

  • Return a brief summary of what has been done to the terminal.

Update the following credentials in the script:

  • uri - Add the server and port to connect to in the format: server:port

  • user - A username for your P4 user.

  • ws_client - The name of the workspace you want to connect to.

  • changelistId - The numbered changelist that contains your checked-out file.

To submit a file using P4 API for .NET:

  1. In Solution Explorer, open Program.cs.

  2. Replace all the code in Program.cs with the following script.

    Copy
    using Perforce.P4;

    string uri = "localhost:1666";
    string user = "P4UserName";
    string ws_client = "projects_main";

    // Changelist number created earlier
    int changelistId = 12345;

    // defines the P4 server, repository and connection
    Server server = new Server(new ServerAddress(uri));
    Repository repo = new Repository(server);
    Connection con = repo.Connection;

    // use the connection variables for this connection
    con.UserName = user;
    con.Client = new Client { Name = ws_client };

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

    // fetch the existing pending changelist
    Changelist changelist = repo.GetChangelist(changelistId);

    // submit the changelist
    SubmitResults results = changelist.Submit(null);

    Console.WriteLine($"Submitted changelist {changelist.Id}");
  3. Build the script (Ctrl + Shift + B).

  4. Run the script (Ctrl + F5).

  5. In the terminal, view the text to check the changelist has been submitted.

  6. (Optional) Open P4V to check the pending changelist is no longer there. This is because it has been submitted.

Next steps

View the documentation for Programming with P4 API for .NET to see what other commands are available when using P4 API for .NET.