Domanda

Interò precompilare un'applicazione ASP.NET nel mio modulo C # personalizzato.Come posso recuperare i registri di processo e verificare se è un processo di successo o no?

Ecco il mio codice

string msPath = "c:\\WINDOWS\\Microsoft.NET\\Framework\\v2.0.50727\\";
string msCompiler = "aspnet_compiler.exe";
string fullCompilerPath = Path.Combine(msPath, msCompiler);
msPath.ThrowIfDirectoryMissing();
fullCompilerPath.ThrowIfFileIsMissing();

ProcessStartInfo process = new ProcessStartInfo 
{ 
    CreateNoWindow = false,
    UseShellExecute = false,
    WorkingDirectory = msPath,
    FileName = msCompiler,
    Arguments = "-p {0} -v / {1}"
        .StrFormat(
            CurrentSetting.CodeSource,
            CurrentSetting.CompileTarget)
};

Process.Start(process);
.

Grazie!

È stato utile?

Soluzione

Imposta il tuo ProcessStartInfo.RedirectStandardOutput a true -Questo reindirizzerà tutte le output su Process.StandardOutput , cheÈ un flusso che puoi leggere per trovare tutti i messaggi di output:

ProcessStartInfo process = new ProcessStartInfo 
{ 
   CreateNoWindow = false,
   UseShellExecute = false,
   WorkingDirectory = msPath,
   RedirectStandardOutput = true,
   FileName = msCompiler,
   Arguments = "-p {0} -v / {1}"
            .StrFormat(
              CurrentSetting.CodeSource, 
              CurrentSetting.CompileTarget)
};

Process p = Process.Start(process);
string output = p.StandardOutput.ReadToEnd();
.

È inoltre possibile utilizzare l'evento OutputDataReceived in modo simile a ciò che @Bharath K descrive nella sua risposta.

Ci sono proprietà / eventi simili per StandardError - è necessario impostare anche RedirectStandardError in true.

Altri suggerimenti

Nel registro dell'applicazione sorgente per l'evento ErrordatarChived:

StringBuilder errorBuilder = new StringBuilder( );
reportProcess.ErrorDataReceived += delegate( object sender, DataReceivedEventArgs e )
{
    errorBuilder.Append( e.Data );
};
//call this before process start
reportProcess.StartInfo.RedirectStandardError = true;
//call this after process start
reportProcess.BeginErrorReadLine( );
.

Qualsiasi errore lanciato nell'applicazione di destinazione può scrivere dati in questo.Qualcosa del genere:

Console.Error.WriteLine( errorMessage ) ;
.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top