I'm writing a program that a user loads a file (txt or csv) in which it reads newlines and commas as new object delimiters. Each new object is put into an array creating a new array object each time. There is also several tests in place to check the data being read into the program to return an error if there are characters that can't be used for a file or folder. There is an option to clean the data being read in which it will read a line of data, then run several replace statements on it to change illegal characters for safe characters. Somewhere in my code it is cleaning the data, replacing the illegal characters then trying to create files off the array objects but even though I've cleaned the data of illegal characters, upon creation, visual studio is spitting out its own error saying illegal characters (not the error I built as a test). Any ideas on why this might be are greatly appreciated. Bellow is the code in question.
and here is the replace statement:
Thanks for everyones help on this.
Code:
private void Runbtn_Click(object sender, EventArgs e)
{//1
string[] lines = null; //create lines array
using (StreamReader sr = new StreamReader(@newobject.csvDirectory))
{//2
string fileContents = sr.ReadToEnd();
lines = fileContents.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
}//2
bool safeLength = true;
if (Filebtn.Checked == true)
{//3
for (int i = 0; i < lines.Length; i++)
{//4
if (lines[i].Length > MAXFILENAME)
{//5
newobject.fileNameError();
safeLength = false;
break;
}//5
}//4
if (safeLength == true)
{//4
if (cleancheckbox.Checked == true)
{ //5
newobject.creationInProgress(); // prompts that files are being made
//create the child form
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 i = 0; i < lines.Length; i++)
{//6
lines[i] = newobject.cleanText(lines[i]); // sends data to get cleaned
//takes the input from the new folder text box
// and assigns it to the filename variable
string fileName = lines[i] + ".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);
}//6
}//5
else if (cleancheckbox.Checked == false)
{
// Return an error for file with illegal characters
bool linesOK = true;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].IndexOfAny(illegal) != -1)
{
newobject.illegalError();
linesOK = false;
break;
}
}
if (linesOK == true)
{
newobject.creationInProgress();
for (int i = 0; i < lines.Length; i++)
{
// 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);
//takes the input from the new folder text box
// and assigns it to the filename variable
string fileName = lines[i] + ".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);
}
}
}
}
}
else if (folderbtn.Checked == true)
{
if (cleancheckbox.Checked == true)
{
// Get the value to be passed
string parentfolder = newfldrbx.Text;
//create the child form
newobject.ParentFolder = parentfolder;
// Specify a "currently active folder"
string outputdir = folderBrowserDialog1.SelectedPath;
//Create a new subfolder under the current active folder
string newParent = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
// Create the subfolder
System.IO.Directory.CreateDirectory(newParent);
for (int i = 0; i < lines.Length; i++)
{
lines[i] = newobject.cleanText(lines[i]);
//Create a new subfolder under the current active folder
string newChild = System.IO.Path.Combine(newParent, lines[i]);
// Create the subfolder
System.IO.Directory.CreateDirectory(newChild);
}
newobject.creationInProgress();
}
else if (cleancheckbox.Checked == false)
{
// Return an error for file with illegal characters
bool linesOK = true;
for (int i = 0; i < lines.Length; i++)
{
if (lines[i].IndexOfAny(illegal) != -1)
{
newobject.illegalError();
linesOK = false;
break;
}
}
// if the file chosen has no illegal characers this this set of
// commmands occurs
if (linesOK == true)
{
// Get the value to be passed
string parentfolder = newfldrbx.Text;
//create the child form
newobject.ParentFolder = parentfolder;
// Specify a "currently active folder"
string outputdir = folderBrowserDialog1.SelectedPath;
//Create a new subfolder under the current active folder
string newParent = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
// Create the subfolder
System.IO.Directory.CreateDirectory(newParent);
for (int k = 0; k < lines.Length; k++)
{
//Create a new subfolder under the current active folder
string newChild = System.IO.Path.Combine(newParent, lines[k]);
// Create the subfolder
System.IO.Directory.CreateDirectory(newChild);
}
// this lets the user know the folders were made
// and must lie outside of the loops to only iterate
// one time
newobject.creationInProgress();
}
}
}
}
Code:
public string cleanText(string c)
{
string cleanText = c.Replace("/", "-");
string cleanText2 = cleanText.Replace("\\", "-");
string cleanText3 = cleanText2.Replace(":", "-");
string cleanText4 = cleanText3.Replace("?", "-");
string cleanText5 = cleanText4.Replace("*", "-");
string cleanText6 = cleanText5.Replace("|", "-");
string cleanText7 = cleanText6.Replace("\"", "");
return cleanText7;
}
Comment