Solved

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

Posted on 2014-03-10
2
287 Views
Last Modified: 2014-03-10
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);
                    }
                }
            }          
        }
0
Comment
Question by:zimmer9
2 Comments
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 250 total points
Comment Utility
i would make a boolean and set it to true after the first time before the method is called do and if check on that variable before executing the method


if(!ran)
    executemethod();

ran = true;
0
 
LVL 25

Accepted Solution

by:
SStory earned 250 total points
Comment Utility
If it isn't in a loop it will only process one time. Or if  you want once per file, I'd keep track of what files I'd already processed in some hash list and check it before calling on a file and not recall if it is in the list.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now