I am writing a program which reads a file into an array creating a member for every line of text. I am trying to create a test that will examine each member for a given list of char's and IF any of the listed char's are found it needs to spit out an error. If no errors are found it needs to go to the next set of instructions. It seems like a simple If Else statement but in order to iterate for every line I'm using a for loop as such:
The problem, if I am understanding correctly, is that having my if else inside the FOR loop is causing it to read every line for the given char's and if it fails the IF statement, it goes to the ELSE and loops repeatedly until the FOR condition is met.
I think a Break; would work to cause it to only run thru the ELSE statement once but I can't figure it out exactly.
If anyone has a solution to this or a recommendation to an alternative method (I toyed with a while loop but got even more lost) I would be very appreciative.
Thanks in advance.
Code:
if (cleancheckbox.Checked == false)
{
// Return an error for file with illegal characters
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].IndexOfAny(illegal) != -1)
{
newobject.illegalError();
break;
}
// If file has no illegal characters this creates the files.
else
{
// stores the string entered for the new folder name
newobject.ParentFolder = newfldrbx.Text;
// Specify a folder to house everything new inside
//This is not the folder that holds the created files/folders
// based off of csv/txt values
string outputdir = folderBrowserDialog1.SelectedPath;
//Create a new subfolder under the current active folder
// this new folder will hold all files/folders created
// per project.
string newPath = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
// Create the subfolder
System.IO.Directory.CreateDirectory(newPath);
for (int j = 0; j < lines.Length; j++)
{
//takes the input from the new folder text box
// and assigns it to the filename variable
string fileName = lines[j] + ".txt";
// creates new file based off of file name input annd
// parent directory
string newFile = System.IO.Path.Combine(newPath, fileName);
System.IO.File.Create(@newFile);
}
I think a Break; would work to cause it to only run thru the ELSE statement once but I can't figure it out exactly.
If anyone has a solution to this or a recommendation to an alternative method (I toyed with a while loop but got even more lost) I would be very appreciative.
Thanks in advance.
Comment