Perforce API for the .Net CLR P4.Net

P4Connection.OnPrompt Event

Raised when Perforce is prompting for a response.

[Visual Basic]
Public Event OnPrompt As OnPromptEventHandler
[C#]
public event OnPromptEventHandler OnPrompt;

Event Data

The event handler receives an argument of type P4PromptEventArgs containing data related to this event. The following P4PromptEventArgs properties provide information specific to this event.

Property Description
Message The message from Perforce.
Response The value to respond to Perforce with.

Remarks

Handle Perforce "prompts". Prompts are from commands that ask the user to respond, such as:

  • login
  • passwd
  • resolve (without an -a* switch)
  • Example

    The following sample will change the user's password.

    public class PasswordSetter
    {
        private P4Connection _p4 = null;
        private string _oldPassword = "";
        private string _newPassword = "";
        PasswordSetter(P4Connection p4)
        {
            _p4 = p4;
        }
    
        public void SetPassword(string OldPassword, string NewPassword)
        {
            OnPromptEventHandler eh = new OnPromptEventHandler(OnPrompt);
            _p4.OnPrompt += eh;
    
            _oldPassword = OldPassword;
            _newPassword = NewPassword;
    
            //run passwd
            P4UnParsedRecordSet r = _p4.RunUnParsed("passwd");
    
            //Clear the event just in case
            _p4.OnPrompt -= eh;
    
            //Clear the passwords from memory
            _newPassword = "";
            _oldPassword = "";
        }
    
        private void OnPrompt(object sender, P4PromptEventArgs e)
        {
            switch (e.Message)
            {
                case ("Enter old password: "):
                    e.Response = _oldPassword;
                    break;
                case ("Enter new password: "):
                    e.Response = _newPassword;
                    break;
                case ("Re-enter new password: "):
                    e.Response = _newPassword;
                    break;
            }
        }
    } 
            

    See Also

    P4Connection Class | P4API Namespace