zimmer9
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.Fu llName, sw1);"
so that it would only execute 1 time?
-------------------------- ---------- ---------- ---------- ---------- ---------- ---------- ------
static void Main()
{
MyGlobals.BASE_FILE_Name = "C" + DateTime.Now.ToString("yyy yMMdd.HHmm ss");
File.Open(MyGlobals.BASE_D IR + MyGlobals.BASE_FILE_Name + ".ard", FileMode.Create).Close();
System.Reflection.Assembly currentRunningAssembly = System.Reflection.Assembly .GetExecut ingAssembl y();
string errorFilePath = System.IO.Path.Combine(MyG lobals.BAS E_DIR, "error.txt");
string indFilePath = System.IO.Path.Combine(MyG lobals.BAS E_DIR, MyGlobals.BASE_FILE_Name + ".ard.ind");
string bankFilePath = System.IO.Path.Combine(MyG lobals.BAS E_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.BA SE_DIR);
foreach (FileInfo file in parentDirectory.GetFiles() )
{
if (!string.Equals(file.Name, MyGlobals.BASE_FILE_Name + ".ard", StringComparison.Invariant CultureIgn oreCase) &&
!string.Equals(GetFileTitl eOnly(file .Name), GetFileTitleOnly(currentRu nningAssem bly.Manife stModule.N ame), StringComparison.Invariant CultureIgn oreCase))
{
StreamWriter sw1 = new StreamWriter(errorFilePath );
ProcessFile(file.FullName, sw1);
ProcessFileBinary(file.Ful lName, sw1); <---- Want to execute 1 time only
}
}
if (File.Exists(errorFilePath ))
{
if (new FileInfo(errorFilePath).Le ngth == 0)
{
if (File.Exists(errorFilePath ))
{
File.Delete(errorFilePath) ;
}
}
}
}
In the following Main routine, how would you modify the statement
"ProcessFileBinary(file.Fu
so that it would only execute 1 time?
--------------------------
static void Main()
{
MyGlobals.BASE_FILE_Name = "C" + DateTime.Now.ToString("yyy
File.Open(MyGlobals.BASE_D
System.Reflection.Assembly
string errorFilePath = System.IO.Path.Combine(MyG
string indFilePath = System.IO.Path.Combine(MyG
string bankFilePath = System.IO.Path.Combine(MyG
//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.BA
foreach (FileInfo file in parentDirectory.GetFiles()
{
if (!string.Equals(file.Name,
!string.Equals(GetFileTitl
{
StreamWriter sw1 = new StreamWriter(errorFilePath
ProcessFile(file.FullName,
ProcessFileBinary(file.Ful
}
}
if (File.Exists(errorFilePath
{
if (new FileInfo(errorFilePath).Le
{
if (File.Exists(errorFilePath
{
File.Delete(errorFilePath)
}
}
}
}
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.