Example console specification fetcher application

This is a console application that connects to a P4 Server and returns P4 specific information based on user input of 2 words: specification type, specification name. It uses Connection.Login(password) for working with a server at security level 3. Lower security levels should also work and connecting with a user without a password can be done by passing a blank string for password. There is minimal error handling for failed P4 commands. Since -o is used, specifications that do not yet exist may be returned on entry of a non-existent specification name.

Create a new C# Console Application project and paste the following code into Program.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Perforce.P4;
namespace apiConsoleApplication
{
	class Program
	{
		static void Main()
		{
			string uri = "";
			string user = "";
			string workspace = "";
			string pass = "";

			bool exit = false;

			Server server = new Server(new ServerAddress(uri));
			Repository rep = new Repository(server);

			// establish a connection to a server
			while(rep.Connection.Status==ConnectionStatus.Disconnected)
			{
				// get user input for connection
				Console.WriteLine("port:");
				uri = Console.ReadLine();
				Console.WriteLine("");
				Console.WriteLine("user:");
				user = Console.ReadLine();
				Console.WriteLine("");
				Console.WriteLine("client:");
				workspace = Console.ReadLine();
				Console.WriteLine("");
				Console.WriteLine("password:");
				pass = Console.ReadLine();
				Console.WriteLine("");

				server = new Server(new ServerAddress(uri));
				rep = new Repository(server);
				Connection con = rep.Connection;
				con.UserName = user;
				con.Client = new Client();
				con.Client.Name = workspace;

				// connect
				bool connected = con.Connect(null);
				if (connected)
				{
					try
					{
						// attempt a login
						Credential cred = con.Login(pass);
					}
					catch (Exception ex)
					{
						Console.WriteLine(ex.Message);
						con.Disconnect();
						continue;
					}
					// get p4 info and show successful connection
					ServerMetaData info = rep.GetServerMetaData(null);
					Console.WriteLine("CONNECTED TO " + info.Address.Uri);
					Console.WriteLine("");
				}
				else
				{
					// retry the prompt for connection info
					continue;
				}
			}
			while(rep.Connection.Status==ConnectionStatus.Connected&&!(exit))
			{
				Options opts= new Options();
				opts["-o"] = null;
				string input = Console.ReadLine();
				exit = (input == "exit");
				string[] command = input.Split(' ');
				switch (command[0])
				{
						// on user input of job <job name> get job
						// and output to console
					case "job":
						try
						{
							Job job = rep.GetJob(command[1], opts);
							Console.WriteLine("");
							Console.WriteLine(job.ToString());
							Console.WriteLine("");
							break;
						}
						catch (Exception ex)
						{
							Console.WriteLine("");
							Console.WriteLine(ex.Message);
							Console.WriteLine("");
							break;
						}
 
						// on user input of change <change number> get
						// change and output to console
					case "change":
						try
						{
							opts = new Options();
							int id = 0;
							int.TryParse(command[1], out id);

							Changelist change = rep.GetChangelist(id, opts);
							Console.WriteLine("");
							Console.WriteLine(change.ToString());
							Console.WriteLine("");
							break;
						}
						catch (Exception ex)
						{
							Console.WriteLine("");
							Console.WriteLine(ex.Message);
							Console.WriteLine("");
							break;
						}
 
						// on user input of client <client name> get
						// client and output to console
					case "client":
						try
						{
							Client client = rep.GetClient(command[1]);
							Console.WriteLine("");
							Console.WriteLine(client.ToString());
							Console.WriteLine("");
							break;
						}
						catch (Exception ex)
						{
							Console.WriteLine("");
							Console.WriteLine(ex.Message);
							Console.WriteLine("");
							break;
						}
 
						// on user input of label <label name> get
						// label and output to console
					case "label":
						try
						{
							Label label = rep.GetLabel(command[1]);
							Console.WriteLine("");
							Console.WriteLine(label.ToString());
							Console.WriteLine("");
							break;
						}
						catch (Exception ex)
						{
							Console.WriteLine("");
							Console.WriteLine(ex.Message);
							Console.WriteLine("");
							break;
						}
 
						// on user input of stream <stream path>get
						// stream and output to console
					case "stream":
						try
						{
							Stream stream = rep.GetStream(command[1]);
							Console.WriteLine("");
							Console.WriteLine(stream.ToString());
							Console.WriteLine("");
							break;
						}
						catch (Exception ex)
						{
							Console.WriteLine("");
							Console.WriteLine(ex.Message);
							Console.WriteLine("");
							break;
						}
 
						// on user input of branch <branch name> get
						// branch and output to console
					case "branch":
						try
						{
							BranchSpec branch = rep.GetBranchSpec(command[1]);
							Console.WriteLine("");
							Console.WriteLine(branch.ToString());
							Console.WriteLine("");
							break;
						}
						catch (Exception ex)
						{
							Console.WriteLine("");
							Console.WriteLine(ex.Message);
							Console.WriteLine("");
							break;
						}
				}
			}
		}
	}
}