When capturing error in command prompt in windows forms application c# is giving me exception

44 views Asked by At

I am trying to execute some encrypt/decrypt command in command prompt from c# and I want to capture the console output if there is any error in command prompt .

I am trying below code

 var p =  Process.Start(
            @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe",
             action + @"  " + section + " " + path + " "                
            

      );
            string errorInfoIfAny = p.StandardError.ReadToEnd().TrimEnd();

The above line givng me error "StandardError has not been redirected."

Paramaters

action= "-peff" (intentionally given for generating error)
section = "appSettings"
path= any folder path

tried below code also but not working

Process p = new Process();
            ProcessStartInfo startInfo = new ProcessStartInfo()
            {
                // CreateNoWindow = true,
                UseShellExecute = false,
                RedirectStandardError = true,
                RedirectStandardOutput = true,
                WorkingDirectory = Environment.CurrentDirectory
            };
            startInfo.FileName = "cmd.exe";
            startInfo.Arguments = @"C:\Windows\Microsoft.NET\Framework\v4.0.30319\aspnet_regiis.exe" + " " +
           action + @"  " + section + " " + path + " "; // cmd.exe spesific implementation
            p.StartInfo = startInfo;
            p.Start();

            string errorInfoIfAny = p.StandardError.ReadToEnd().TrimEnd();

Can anybody please help me on this ?

Thanks

0

There are 0 answers