Reading system .Diagnostics.ProcessStartInfo StandardOutput as bytes instead of characters

I am trying to automate svnadmin dump using C # ProcessStartInfo.

Like I did on the command line, so,

svnadmin dump c:\Repositories\hackyhacky > c:\backup\hackyhacky.svn_dump

Works with processing and dumps successfully, and I can verify this by restoring it to another repository like

svnadmin load c:\Repositories\restore_test < c:\backup\hackyhacky.svn_dump

What is restored successfully - yay!

Now ... I need to replicate the command line pipeline to another file using C #, but for some reason

var startInfo = new ProcessStartInfo(Path.Combine(SvnPath, "svnadmin"),"dump c:\Repositories\hackyhacky")
 {CreateNoWindow = true, RedirectStandardOutput = true,RedirectStandardError = true,UseShellExecute = false};
process.StartInfo = startInfo;
process.Start();
StreamReader reader = process.StandardOutput;
char[] standardOutputCharBuffer = new char[4096];
byte[] standardOutputByteBuffer;
int readChars = 0;
long totalReadBytes = 0;

// read from the StandardOutput, and write directly into another file

using (StreamWriter writer = new StreamWriter(@"C:\backup\hackyhacky.svn_dump", false)) {
    while (!reader.EndOfStream) {
       // read some chars from the standard out
       readChars = reader.Read(standardOutputCharBuffer, 0, standardOutputCharBuffer.Length);

       // convert the chars into bytes
       standardOutputByteBuffer = reader.CurrentEncoding.GetBytes(standardOutputCharBuffer);

       // write the bytes out into the file
       writer.Write(standardOutputCharBuffer.Take(readChars).ToArray());

       // increment the total read
       totalReadBytes += standardOutputByteBuffer.Length;
    }                    
}

Flushes the same repo in hackyhacky.svn_dump.

But when I run my boot command line

svnadmin load c:\Repositories\restore_test < c:\backup\hackyhacky.svn_dump

I get weird-error checksum error!

svnadmin load c:\Repositories\restore_test < c:\backup\hackyhacky.svn_dump
< Started new transaction, based on original revision 1
     * adding path : Dependencies ... done.
     * adding path : Dependencies/BlogML.dll ...svnadmin: Checksum mismatch, fil
e '/Dependencies/BlogML.dll':
   expected:  d39863a4c14cf053d01f636002842bf9
     actual:  d19831be151d33650b3312a288aecadd

I assume this is due to the way I redirect and read StandardOutput.

Does anyone know the correct way to simulate the behavior of strings on the command line in C #?

Any help is greatly appreciated.

-cv

UPDATE

BinaryWriter standardOutputByteBuffer , . - .

+3
3

! , ...

, Process StartInfo , .

http://weblogs.asp.net/israelio/archive/2004/08/31/223447.aspx

,

http://webcache.googleusercontent.com/search?q=cache:http://www.deadbeef.com/index.php/redirecting_the_output_of_a_program_to_a

, ...

amWriter bat = File.CreateText("foo.bat"); 
bat.WriteLine("@echo off"); 
bat.WriteLine("foo.exe -arg >" + dumpDir + "\\foo_arg.txt"); 
bat.Close(); 
Process task = new Process(); 
task.StartInfo.UseShellExecute = false; 
task.StartInfo.FileName = "foo.bat"; 
task.StartInfo.Arguments = ""; 
task.Start(); 
task.WaitForExit();

:

, !

, , , , , .

+1

, , - - .

, . , : UTF-8, ( ) Windows-1252.

0

, Hector Sosa svnmanagerlib sourceforge:

The key to solving this issue was calling WaitForExit () with file operations. You also need to make sure that the output is written to disk. Here are the relevant lines:

File.AppendAllText (destinationFile, myOutput.ReadToEnd ()); svnCommand.WaitForExit (); File.AppendAllText (destinationFile, myOutput.ReadToEnd ());

Note that I am making a call to File.AppendAllText () twice. I found that the output stream does not write everything during the first call in File.AppendAllText () in some cases.

public static bool ExecuteWritesToDiskSvnCommand(string command, string arguments, string destinationFile, out string errors)
        {
            bool retval = false;
            string errorLines = string.Empty;
            Process svnCommand = null;
            ProcessStartInfo psi = new ProcessStartInfo(command);

            psi.RedirectStandardOutput = true;
            psi.RedirectStandardError = true;
            psi.WindowStyle = ProcessWindowStyle.Hidden;
            psi.UseShellExecute = false;
            psi.CreateNoWindow = true;

            try
            {
                Process.Start(psi);
                psi.Arguments = arguments;
                svnCommand = Process.Start(psi);

                StreamReader myOutput = svnCommand.StandardOutput;
                StreamReader myErrors = svnCommand.StandardError;

                File.AppendAllText(destinationFile, myOutput.ReadToEnd());
                svnCommand.WaitForExit();
                File.AppendAllText(destinationFile, myOutput.ReadToEnd());

                if (svnCommand.HasExited)
                {
                    errorLines = myErrors.ReadToEnd();
                }

                // Check for errors
                if (errorLines.Trim().Length == 0)
                {
                    retval = true;
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                errorLines += Environment.NewLine + msg;
            }
            finally
            {
                if (svnCommand != null)
                {
                    svnCommand.Close();
                }
            }

            errors = errorLines;

            return retval;
        }
0
source

Source: https://habr.com/ru/post/1759737/


All Articles