Link to home
Start Free TrialLog in
Avatar of zimmer9
zimmer9Flag for United States of America

asked on

How to execute a method 1 time in a FOR loop within a C# Console application?

I am writing a C# Console applicaiton.

In the following Main routine, how would you modify the statement

"ProcessFileBinary(file.FullName, sw1);"

so that it would only execute 1 time?

--------------------------------------------------------------------------------------------

static void Main()
        {        
            MyGlobals.BASE_FILE_Name = "C" + DateTime.Now.ToString("yyyyMMdd.HHmmss");
            File.Open(MyGlobals.BASE_DIR + MyGlobals.BASE_FILE_Name + ".ard", FileMode.Create).Close();
            System.Reflection.Assembly currentRunningAssembly = System.Reflection.Assembly.GetExecutingAssembly();
            string errorFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, "error.txt");
            string indFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
            string bankFilePath = System.IO.Path.Combine(MyGlobals.BASE_DIR, MyGlobals.BASE_FILE_Name + ".ard.out");
            //delete the error file if it presently exists so that a new error file can be created in the event any errors are discovered
            if (File.Exists(errorFilePath))
            {
                File.Delete(errorFilePath);
            }
            //delete the bank output file if it presently exists so that a new bank output file can be created in the event any errors are discovered
            if (File.Exists(indFilePath))
            {
                File.Delete(indFilePath);
            }
            //delete the bank check image file if it presently exists so that a new bank check image file can be created in the event any errors are discovered
            if (File.Exists(bankFilePath))
            {
                File.Delete(bankFilePath);
            }
            //the incoming check image file does not have a file extension
            DirectoryInfo parentDirectory = new DirectoryInfo(MyGlobals.BASE_DIR);

            foreach (FileInfo file in parentDirectory.GetFiles())
            {
                if (!string.Equals(file.Name, MyGlobals.BASE_FILE_Name + ".ard", StringComparison.InvariantCultureIgnoreCase) &&
                    !string.Equals(GetFileTitleOnly(file.Name), GetFileTitleOnly(currentRunningAssembly.ManifestModule.Name), StringComparison.InvariantCultureIgnoreCase))
                {
                    StreamWriter sw1 = new StreamWriter(errorFilePath);
                    ProcessFile(file.FullName, sw1);
                    ProcessFileBinary(file.FullName, sw1);     <---- Want to execute 1 time only
                }
            }
            if (File.Exists(errorFilePath))
            {
                if (new FileInfo(errorFilePath).Length == 0)
                {
                    if (File.Exists(errorFilePath))
                    {

                        File.Delete(errorFilePath);
                    }
                }
            }          
        }
SOLUTION
Avatar of p_davis
p_davis

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial