Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-03-10
2
Medium Priority
?
294 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 22

Assisted Solution

by:p_davis
p_davis earned 1000 total points
ID: 39918023
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 1000 total points
ID: 39918032
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

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…

715 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