Search code examples
c#asp.netstdoutstdinperforce

proc.StandardOutput.ReadLine() returns empty string when I am going to run a p4 (Perforce) command from my c# code


I have a working code in C# (of course in another machine) which after migration, it does not anymore. The code runs a command toward a perforce server in command prompt and reads the output looking for a specific string. the code is:

string result="";
            System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
            psi.UseShellExecute = false;
            psi.RedirectStandardOutput = true;
            psi.RedirectStandardInput = true;
            psi.RedirectStandardError = true;

            //Start the process
            System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
            //Attach output for reading
            System.IO.StreamReader sOut = proc.StandardOutput;
            System.IO.StreamWriter sIn = proc.StandardInput;

            sIn.WriteLine(@"p4 changelists -m 1 -s submitted //<SOME_PATH_HERE>");
            proc.WaitForExit(500);
            sIn.WriteLine("EXIT");

            while((result=sOut.ReadLine()) != null)
            {
                if(result != "")
                {
                    if(result.Substring(0, 6) == "Change")
                    {
                        //get Changelist number
                        result = result.Substring(7, 6);
                        break;
                    }
                }
            }
            if((result == "") || (result == null))  //if perforce goes down
            {

the problem is when i issue some cmd.exe wellknown commands such as DIR and ... I can see the out put line by line in my result variable but for this special command of p4 the result string is empty.

I googled my problem and the closest thing that might be related was something to do with CASPOL !?

I don't have any idea of what it is exaclty and how can I get rid of it.

any help would be appreciated.


Solution

  • I'm not familiar enough with C# to be able to debug the RedirectStandardError part of your code, but usually if you're scripting a p4 command and it appears to fail silently it means you're not capturing stderr.

    I can see your code capturing stdout explicitly; does it maybe need to do something like this?

    System.IO.StreamReader sErr = proc.StandardError;
    ...
    while((result=sErr.ReadLine()) != null)
       ...
    

    I notice that your code doesn't provide any connection information, so my guess as to why it's failing after migration is that it was dependent on authentication/connection settings in the old machine's environment (like valid values for P4USER and P4PORT and a P4TICKETS file with a valid authentication ticket). If you're able to get stderr from the failing command it will give you more information (like "can't connect to server" or "authentication failed" or "user unknown").