1

我正在使用 p4api.net 编写一个 VS C# 应用程序来访问 P4 服务器。P4V 应用程序使用给定的用户/密码访问存款罚款。使用 p4api.net API,代码使用从表单传入的服务器/用户/密码字符串无一例外地执行 Connect() 和 Login() 方法:

  rep.Connection.Connect(options);
  rep.Connection.Login(password, options);

下面是实际代码:

String conStr = mServerConnection.Text;
String user = mUserText.Text;
String password = mPaswordTxt.Text;
try
{
   Server server = new Server(new ServerAddress(conStr));
   rep = new Repository(server);
   rep.Connection.UserName = user;
   Options options = new Options();
   Options["Password"] = password;
   rep.Connection.Client = new Client();
   rep.Connection.Connect(options);
   rep.Connection.Login(password, options);
}
catch (Exception ex)
{
   rep = null;
   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

然而,在另一个访问 P4“//depot”根目录的调用期间,API 调用 _repository.GetDepotDirs() 将返回“Perforce Password (P4PASSWD) invalid or unset”异常。请看下面的代码:

public bool Expand()
{
  if (String.IsNullOrEmpty(depotPath))
     return false;
  // if we have the depot path, get a list of the subdirectories from the depot
  if (!String.IsNullOrEmpty(depotPath))
  {
     IList<string> subdirs = _repository.GetDepotDirs(null, String.Format("{0}/*", depotPath));

我在某处读到需要设置环境变量 P4TICKETS,所以我在 DOS 提示符下进行了设置:

p4 set P4TICKETS=C:\Documents and Settings\my_user_name

但这并没有解决问题。我会很感激你的帮助。谢谢。

4

1 回答 1

2

在安全级别 3 下,直接将密码提供给Connect不起作用。所以我捕获了随后的异常,然后调用Login。此时,您可以在选项的Ticket字段下保存Login返回的Credential.Ticket以供将来调用Connect

我不确定为什么后续的命令调用对您而言失败,除非您的票证以某种方式过期,或者您实际上已与服务器断开连接。您始终可以在后续命令之前再次 调用Connect 。

于 2012-01-08T03:52:14.857 回答