using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Perforce.P4;
using System.Diagnostics;
using System.IO;
namespace SampleApp
{
public partial class MainForm : Form
{
string uri;
string user;
string clientName;
string password;
string clientMap = "ClientMap.txt";
public MainForm()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void ExitButton_Click(object sender, EventArgs e)
{
Application.Exit();
}
private void ConnectButton_Click(object sender, EventArgs e)
{
// Connection details.
uri = "public.perforce.com:1666";
uri = @"rsh:c:\test\p4d.exe -r C:\test -L log -i";
user = "Paul_Arva";
clientName = "Paul_Arva_SampleApp";
password = "Perpass1";
// Define server, repository and connection.
Server server = new Server(new ServerAddress(uri));
Repository rep = new Repository(server);
Connection con = rep.Connection;
// Define username and client name.
con.UserName = user;
con.Client = new Client();
con.Client.Name = clientName;
try
{
// Connect to server.
con.Connect(null);
// Successful connection.
StatusLabel.Text = "Connected";
StatusLabel.ForeColor = Color.Green;
// Login to server.
Credential cred = con.Login(password, null, null);
LoginLabel.Text = "Logged in";
LoginLabel.ForeColor = Color.Green;
// Get client spec.
Client client = rep.GetClient(clientName);
// Show client mapping file.
if (!System.IO.File.Exists(clientMap))
{
System.IO.File.Create(clientMap);
}
TextWriter tw = new StreamWriter(clientMap);
tw.WriteLine(client.ViewMap.ToString());
tw.Close();
Process clientMapProcess = Process.Start(clientMap);
}
catch (P4CommandTimeOutException ex)
{
MessageBox.Show(ex.Message, "Command Timeout");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
if (con.Status == ConnectionStatus.Connected) con.Disconnect();
}
}
}
}