zimmer9
asked on
How would you abort a C# Windows platform application if an array were to exceeds its boundaries "Index was outside the bounds of the array"?
I am developing a C# application using VS2005 on a windows platform. How would you modify the following snippet of code to end the program immediately if arr1 > 99000
and write the error "Index was outside the bounds of the array" to the file Check.err.txt
string[,] arr1 = new string[99000, 20];
-------------------------- ---------- ---------- -------
using System;
using System.Xml;
using System.IO;
using System.Text.RegularExpress ions;
using System.Globalization;
using System.Collections.Generic ;
namespace ReadXml1
{
class Class1
{
static void Main(string[] args)
{
string goodFilePath = @"\\v\r7.1.ard.ind";
string errorFilePath = @"\\v\Check.err.txt";
if (File.Exists(goodFilePath) )
{
File.Delete(goodFilePath);
}
if (File.Exists(errorFilePath ))
{
File.Delete(errorFilePath) ;
}
DirectoryInfo parentDirectory = new DirectoryInfo(@"\\v\r");
bool hasElseBeenExecuted = false; // <-- add this boolean line to use as a check further down
foreach (FileInfo file in parentDirectory.GetFiles() )
{
StreamWriter sw = null;
try
{
if (file != null && XmlFile(file.FullName))
{
StreamWriter sw1 = new StreamWriter(errorFilePath );
sw = new StreamWriter(goodFilePath) ;
ProcessFile(file.FullName, sw, sw1);
}
else if (!hasElseBeenExecuted) // <-- change this from a regular "else" to an "else if" that specifically looks to see if the hasElseBeenExecuted variable is set to false
{
hasElseBeenExecuted = true; // <-- set the boolean flag to true so that this else block never gets hit again
sw = new StreamWriter(emptyFilePath );
File.Copy(file.FullName, @"\\v\region\na\appl\ctrls \cashcontr ol\data\pr od\HarrisC heck\data\ HRSBK.HARR ISBK.CHKHA RB4.CHKHAR B4.77.1.ar d.out");
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Messa ge);
}
finally
{
if (sw != null) sw.Close();
}
}
if (File.Exists(errorFilePath ))
{
if (new FileInfo(errorFilePath).Le ngth == 0)
{
if (File.Exists(errorFilePath ))
{
File.Delete(errorFilePath) ;
}
}
else
{
if (File.Exists(goodFilePath) )
File.Delete(goodFilePath);
}
}
Console.WriteLine("Process ing done. Press enter to exit ... ");
Console.ReadLine();
}
private static void ProcessFile(string filePath, StreamWriter sw, StreamWriter sw1)
{
int i = 0;
string[,] arr1 = new string[99000, 20]; //where array can exceed 99,000 records
int iCheckNumber = 0;
String formattedString1 = null;
String itemsequence = null;
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(filePath);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "csc:item_sequence_number" )
{
arr1[i, 11] = "GROUP_FIELD_NAME:CpcsNo";
itemsequence = reader.ReadElementContentA sString();
String formattedString = itemsequence.Substring(7, 8);
Int64 intTest = 0;
if (Int64.TryParse(formattedS tring, out intTest) && intTest > 0)
arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
else
{
arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
sw1.WriteLine("GROUP_FIELD _NAME:Item Sequence:" + itemsequence + ",GROUP_FIELD_NAME:CpcsNo" + "," + "GROUP_FIELD_VALUE:" + formattedString);
iCheckNumber--;
}
}
if (reader.Name == "csc:check_number")
{
iCheckNumber = iCheckNumber + 1;
arr1[i, 0] = "COMMENT: CHECK NUMBER #" + iCheckNumber;
arr1[i, 1] = "GROUP_FIELD_NAME:CheckNum ber";
String e = reader.ReadElementContentA sString();
String formattedString = e.Substring(6, 3);
formattedString1 = e.Substring(9, 6);
Int64 intTest = 0;
if (Int64.TryParse(formattedS tring1, out intTest))
arr1[i, 2] = "GROUP_FIELD_VALUE:" + formattedString1;
else
{
arr1[i, 2] = "GROUP_FIELD_VALUE:" + formattedString1;
sw1.WriteLine("GROUP_FIELD _NAME:Item Sequence:" + itemsequence + ",GROUP_FIELD_NAME:CheckNu mber" + "," + "GROUP_FIELD_VALUE:" + formattedString1);
}
arr1[i, 15] = "GROUP_FIELD_NAME:OfficeNo ";
if (Int64.TryParse(formattedS tring, out intTest) && intTest > 0)
arr1[i, 16] = "GROUP_FIELD_VALUE:" + formattedString;
else
{
arr1[i, 16] = "GROUP_FIELD_VALUE:" + formattedString;
sw1.WriteLine("GROUP_FIELD _NAME:Item Sequence:" + itemsequence + ",GROUP_FIELD_NAME:OfficeN o" + "," + "GROUP_FIELD_VALUE:" + formattedString);
}
}
sw.WriteLine("COMMENT: specify code page of the index date");
sw.WriteLine("CODEPAGE:819 ");
for (int iter = 0; iter < i; iter++)
{
for (int j = 0; j < 20; j++)
{
sw.WriteLine(arr1[iter, j]);
}
}
}
finally
{
if (sw1 != null) sw1.Close();
reader.Close();
}
}
static bool XmlFile(string filePath)
{
string line;
bool xmlFile = false;
StreamReader sr = null;
try
{
sr = new StreamReader(filePath);
line = sr.ReadLine();
if (line.Equals("<?xml version=\"1.0\"?>"))
xmlFile = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Messa ge);
xmlFile = false;
}
finally
{
if (sr != null) sr.Close();
}
return xmlFile;
}
}
}
and write the error "Index was outside the bounds of the array" to the file Check.err.txt
string[,] arr1 = new string[99000, 20];
--------------------------
using System;
using System.Xml;
using System.IO;
using System.Text.RegularExpress
using System.Globalization;
using System.Collections.Generic
namespace ReadXml1
{
class Class1
{
static void Main(string[] args)
{
string goodFilePath = @"\\v\r7.1.ard.ind";
string errorFilePath = @"\\v\Check.err.txt";
if (File.Exists(goodFilePath)
{
File.Delete(goodFilePath);
}
if (File.Exists(errorFilePath
{
File.Delete(errorFilePath)
}
DirectoryInfo parentDirectory = new DirectoryInfo(@"\\v\r");
bool hasElseBeenExecuted = false; // <-- add this boolean line to use as a check further down
foreach (FileInfo file in parentDirectory.GetFiles()
{
StreamWriter sw = null;
try
{
if (file != null && XmlFile(file.FullName))
{
StreamWriter sw1 = new StreamWriter(errorFilePath
sw = new StreamWriter(goodFilePath)
ProcessFile(file.FullName,
}
else if (!hasElseBeenExecuted) // <-- change this from a regular "else" to an "else if" that specifically looks to see if the hasElseBeenExecuted variable is set to false
{
hasElseBeenExecuted = true; // <-- set the boolean flag to true so that this else block never gets hit again
sw = new StreamWriter(emptyFilePath
File.Copy(file.FullName, @"\\v\region\na\appl\ctrls
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Messa
}
finally
{
if (sw != null) sw.Close();
}
}
if (File.Exists(errorFilePath
{
if (new FileInfo(errorFilePath).Le
{
if (File.Exists(errorFilePath
{
File.Delete(errorFilePath)
}
}
else
{
if (File.Exists(goodFilePath)
File.Delete(goodFilePath);
}
}
Console.WriteLine("Process
Console.ReadLine();
}
private static void ProcessFile(string filePath, StreamWriter sw, StreamWriter sw1)
{
int i = 0;
string[,] arr1 = new string[99000, 20]; //where array can exceed 99,000 records
int iCheckNumber = 0;
String formattedString1 = null;
String itemsequence = null;
XmlTextReader reader = null;
try
{
reader = new XmlTextReader(filePath);
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
if (reader.Name == "csc:item_sequence_number"
{
arr1[i, 11] = "GROUP_FIELD_NAME:CpcsNo";
itemsequence = reader.ReadElementContentA
String formattedString = itemsequence.Substring(7, 8);
Int64 intTest = 0;
if (Int64.TryParse(formattedS
arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
else
{
arr1[i, 12] = "GROUP_FIELD_VALUE:" + formattedString;
sw1.WriteLine("GROUP_FIELD
iCheckNumber--;
}
}
if (reader.Name == "csc:check_number")
{
iCheckNumber = iCheckNumber + 1;
arr1[i, 0] = "COMMENT: CHECK NUMBER #" + iCheckNumber;
arr1[i, 1] = "GROUP_FIELD_NAME:CheckNum
String e = reader.ReadElementContentA
String formattedString = e.Substring(6, 3);
formattedString1 = e.Substring(9, 6);
Int64 intTest = 0;
if (Int64.TryParse(formattedS
arr1[i, 2] = "GROUP_FIELD_VALUE:" + formattedString1;
else
{
arr1[i, 2] = "GROUP_FIELD_VALUE:" + formattedString1;
sw1.WriteLine("GROUP_FIELD
}
arr1[i, 15] = "GROUP_FIELD_NAME:OfficeNo
if (Int64.TryParse(formattedS
arr1[i, 16] = "GROUP_FIELD_VALUE:" + formattedString;
else
{
arr1[i, 16] = "GROUP_FIELD_VALUE:" + formattedString;
sw1.WriteLine("GROUP_FIELD
}
}
sw.WriteLine("COMMENT: specify code page of the index date");
sw.WriteLine("CODEPAGE:819
for (int iter = 0; iter < i; iter++)
{
for (int j = 0; j < 20; j++)
{
sw.WriteLine(arr1[iter, j]);
}
}
}
finally
{
if (sw1 != null) sw1.Close();
reader.Close();
}
}
static bool XmlFile(string filePath)
{
string line;
bool xmlFile = false;
StreamReader sr = null;
try
{
sr = new StreamReader(filePath);
line = sr.ReadLine();
if (line.Equals("<?xml version=\"1.0\"?>"))
xmlFile = true;
}
catch (Exception ex)
{
Console.WriteLine(ex.Messa
xmlFile = false;
}
finally
{
if (sr != null) sr.Close();
}
return xmlFile;
}
}
}
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.