Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1067089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:06:31+00:00 2026-05-16T20:06:31+00:00

I am using following code to write the PATH, EXECUTABLE NAME and ARGUMENTS to

  • 0

I am using following code to write the PATH, EXECUTABLE NAME and ARGUMENTS to a batch file and execute it through CMD using c#. The problem is sometimes the application dosent starts up after executing the batch file. And the c# code dosent give me exception or any notification.

For which i want to get the Exitcode from CMD to determine if the commands executed properly.
How can i determine Exit code ?

  public void Execute()
{
    try
    {
        string LatestFileName = GetLastWrittenBatchFile();
        if (System.IO.File.Exists(BatchPath + LatestFileName))
        {
            System.Diagnostics.ProcessStartInfo procinfo = new System.Diagnostics.ProcessStartInfo("cmd.exe");
            procinfo.UseShellExecute = false;
            procinfo.RedirectStandardError = true;
            procinfo.RedirectStandardInput = true;
            procinfo.RedirectStandardOutput = true;

            System.Diagnostics.Process process = System.Diagnostics.Process.Start(procinfo); 
            System.IO.StreamReader stream = System.IO.File.OpenText(BatchPath + LatestFileName);
            System.IO.StreamReader sroutput = process.StandardOutput;
            System.IO.StreamWriter srinput = process.StandardInput;


            while (stream.Peek() != -1)
            {
                srinput.WriteLine(stream.ReadLine());
            }

            Log.Flow_writeToLogFile("Executed .Bat file : " + LatestFileName);

            process.WaitForExit(1000);

            if (process.ExitCode != 0)
            {
                int iExitCode = process.ExitCode;
            }
            stream.Close();
            process.Close();
            srinput.Close();
            sroutput.Close(); 
        }
        else
        {
            ExceptionHandler.writeToLogFile("File not found");
        }
    }
    catch (Exception ex)
    {
        ExceptionHandler.writeToLogFile(System.Environment.NewLine + "Target  :  " + ex.TargetSite.ToString() + System.Environment.NewLine + "Message :  " + ex.Message.ToString() + System.Environment.NewLine + "Stack   :  " + ex.StackTrace.ToString());
    }
} 

_________________Update___________________

script inside Batchfile : [Note that Notepads.exe is wrong to get the error ]

START
Notepads.EXE

“if “%ERRORLEVEL%” == “1” exit /B 1″

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:06:31+00:00Added an answer on May 16, 2026 at 8:06 pm

    It is much easier to run the process directly instead of using creating a batch file that you later execute since you lose some control since you are using a batch script layer.

    Use this code instead:

        /// <summary>
        /// Execute external process.
        /// Block until process has terminated.
        /// Capture output.
        /// </summary>
        /// <param name="binaryFilename"></param>
        /// <param name="arguments"></param>
        /// <param name="currentDirectory"></param>
        /// <param name="priorityClass">Priority of started process.</param>
        /// <returns>stdout output.</returns>
        public static string ExecuteProcess(string binaryFilename, string arguments, string currentDirectory, ProcessPriorityClass priorityClass)
        {
            if (String.IsNullOrEmpty(binaryFilename))
            {
                return "no command given.";
            }
    
            Process p = new Process();
            p.StartInfo.FileName = binaryFilename;
            p.StartInfo.Arguments = arguments;
            p.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;
            p.StartInfo.UseShellExecute = false;
            if (!String.IsNullOrEmpty(currentDirectory))
                p.StartInfo.WorkingDirectory = currentDirectory;
            p.StartInfo.CreateNoWindow = false;
    
            p.Start();
            // Cannot set priority process is started.
            p.PriorityClass = priorityClass;
    
            // Must have the readToEnd BEFORE the WaitForExit(), to avoid a deadlock condition
            string output = p.StandardOutput.ReadToEnd();
            p.WaitForExit();
    
            if (p.ExitCode != 0)
            {
                throw new Exception(String.Format("Process '{0} {1}' ExitCode was {2}",
                                     binaryFilename,
                                     arguments,
                                     p.ExitCode));   
            }
            //string standardError = p.StandardError.ReadToEnd();
            //if (!String.IsNullOrEmpty(standardError))
            //{
            //    throw new Exception(String.Format("Process '{0} {1}' StandardError was {2}",
            //                         binaryFilename,
            //                         arguments,
            //                         standardError));
            //}
    
            return output;
        }
    

    I use it in a number of projects and it works like a charm.

    If you HAVE to go the batch script route, make sure that the batch script set exitcode properly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using the following code to write to a file int main(int argc, char *
I'm using the following code to write data through a named pipe from one
I want to write a .xml file using the following code into the App_Data/posts.
I am using following code to write json to my local path which i
I am using the following code to determine if I can write to a
I am using the following code to resize and save the file in to
I am using the following tcl code to store a file from my deskstop
I'm creating an SVG path using the Raphael library and the following code: this.resultsBoxLine1
I am using the following code in iPhone to write a string into the
So I have the following code to take a Excel file and write it

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.