473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Illegal Char error after illegal char's are removed.

110 100+
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.

Expand|Select|Wrap|Line Numbers
  1.  private void Runbtn_Click(object sender, EventArgs e)
  2.  
  3.         {//1
  4.             string[] lines = null; //create lines array
  5.  
  6.             using (StreamReader sr = new StreamReader(@newobject.csvDirectory))
  7.             {//2
  8.                 string fileContents = sr.ReadToEnd();
  9.                 lines = fileContents.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  10.             }//2
  11.  
  12.             bool safeLength = true;
  13.  
  14.             if (Filebtn.Checked == true)
  15.             {//3                
  16.  
  17.                 for (int i = 0; i < lines.Length; i++)
  18.                 {//4
  19.                     if (lines[i].Length > MAXFILENAME)
  20.                     {//5
  21.                         newobject.fileNameError();
  22.                         safeLength = false;
  23.                         break;
  24.                     }//5
  25.  
  26.                 }//4
  27.  
  28.                 if (safeLength == true)
  29.                 {//4
  30.  
  31.                     if (cleancheckbox.Checked == true)
  32.                     { //5                                                           
  33.                         newobject.creationInProgress(); // prompts that files are being made                    
  34.  
  35.                         //create the child form
  36.                         newobject.ParentFolder = newfldrbx.Text;
  37.  
  38.                         // Specify a folder to house everything new inside
  39.                         //This is not the folder that holds the created files/folders
  40.                         // based off of csv/txt values
  41.                         string outputdir = folderBrowserDialog1.SelectedPath;
  42.  
  43.                         //Create a new subfolder under the current active folder
  44.                         // this new folder will hold all files/folders created
  45.                         // per project.
  46.                         string newPath = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  47.  
  48.                         // Create the subfolder
  49.                         System.IO.Directory.CreateDirectory(newPath);
  50.  
  51.                         for (int i = 0; i < lines.Length; i++)
  52.                         {//6
  53.                             lines[i] = newobject.cleanText(lines[i]); // sends data to get cleaned
  54.  
  55.  
  56.                             //takes the input from the new folder text box
  57.                             // and assigns it to the filename variable
  58.                             string fileName = lines[i] + ".txt";
  59.  
  60.                             // creates new file based off of file name input annd
  61.                             // parent directory
  62.                             string newFile = System.IO.Path.Combine(newPath, fileName);
  63.                             System.IO.File.Create(@newFile);
  64.                         }//6
  65.                     }//5
  66.  
  67.                     else if (cleancheckbox.Checked == false)
  68.                     {
  69.  
  70.                         // Return an error for file with illegal characters
  71.  
  72.                         bool linesOK = true;
  73.                         for (int i = 0; i < lines.Length; i++)
  74.                         {
  75.                             if (lines[i].IndexOfAny(illegal) != -1)
  76.                             {
  77.                                 newobject.illegalError();
  78.                                 linesOK = false;
  79.                                 break;
  80.                             }
  81.                         }
  82.  
  83.                         if (linesOK == true)
  84.                         {
  85.                             newobject.creationInProgress();
  86.  
  87.                             for (int i = 0; i < lines.Length; i++)
  88.                             {
  89.                                 // stores the string entered for the new folder name
  90.                                 newobject.ParentFolder = newfldrbx.Text;
  91.  
  92.                                 // Specify a folder to house everything new inside
  93.                                 //This is not the folder that holds the created files/folders
  94.                                 // based off of csv/txt values
  95.                                 string outputdir = folderBrowserDialog1.SelectedPath;
  96.  
  97.                                 //Create a new subfolder under the current active folder
  98.                                 // this new folder will hold all files/folders created
  99.                                 // per project.
  100.                                 string newPath = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  101.  
  102.                                 // Create the subfolder
  103.                                 System.IO.Directory.CreateDirectory(newPath);
  104.  
  105.                                 //takes the input from the new folder text box
  106.                                 // and assigns it to the filename variable
  107.                                 string fileName = lines[i] + ".txt";
  108.  
  109.                                 // creates new file based off of file name input annd
  110.                                 // parent directory
  111.                                 string newFile = System.IO.Path.Combine(newPath, fileName);
  112.                                 System.IO.File.Create(@newFile);
  113.                             }
  114.                         }
  115.                     }
  116.                 }
  117.             }
  118.  
  119.  
  120.             else if (folderbtn.Checked == true)
  121.             {
  122.                 if (cleancheckbox.Checked == true)
  123.                 {
  124.  
  125.  
  126.                     // Get the value to be passed
  127.                     string parentfolder = newfldrbx.Text;
  128.  
  129.                     //create the child form
  130.                     newobject.ParentFolder = parentfolder;
  131.  
  132.                     // Specify a "currently active folder"
  133.                     string outputdir = folderBrowserDialog1.SelectedPath;
  134.  
  135.                     //Create a new subfolder under the current active folder
  136.                     string newParent = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  137.  
  138.                     // Create the subfolder
  139.                     System.IO.Directory.CreateDirectory(newParent);
  140.  
  141.  
  142.                     for (int i = 0; i < lines.Length; i++)
  143.                     {
  144.                         lines[i] = newobject.cleanText(lines[i]);
  145.  
  146.                         //Create a new subfolder under the current active folder
  147.                         string newChild = System.IO.Path.Combine(newParent, lines[i]);
  148.  
  149.                         // Create the subfolder
  150.                         System.IO.Directory.CreateDirectory(newChild);
  151.                     }
  152.  
  153.                     newobject.creationInProgress();
  154.                 }
  155.  
  156.                 else if (cleancheckbox.Checked == false)
  157.                 {
  158.  
  159.  
  160.                     // Return an error for file with illegal characters
  161.                     bool linesOK = true;
  162.                     for (int i = 0; i < lines.Length; i++)
  163.                     {
  164.                         if (lines[i].IndexOfAny(illegal) != -1)
  165.                         {
  166.                             newobject.illegalError();
  167.                             linesOK = false;
  168.                             break;
  169.                         }
  170.                     }
  171.  
  172.                     // if the file chosen has no illegal characers this this set of
  173.                     // commmands occurs
  174.                     if (linesOK == true)
  175.                     {
  176.  
  177.                         // Get the value to be passed
  178.                         string parentfolder = newfldrbx.Text;
  179.  
  180.                         //create the child form
  181.                         newobject.ParentFolder = parentfolder;
  182.  
  183.                         // Specify a "currently active folder"
  184.                         string outputdir = folderBrowserDialog1.SelectedPath;
  185.  
  186.                         //Create a new subfolder under the current active folder
  187.                         string newParent = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  188.  
  189.                         // Create the subfolder
  190.                         System.IO.Directory.CreateDirectory(newParent);
  191.  
  192.                         for (int k = 0; k < lines.Length; k++)
  193.                         {
  194.                             //Create a new subfolder under the current active folder
  195.                             string newChild = System.IO.Path.Combine(newParent, lines[k]);
  196.  
  197.                             // Create the subfolder
  198.                             System.IO.Directory.CreateDirectory(newChild);
  199.  
  200.                         }
  201.                         // this lets the user know the folders were made
  202.                         // and must lie outside of the loops to only iterate
  203.                         // one time
  204.                         newobject.creationInProgress();
  205.  
  206.                     }
  207.                 }
  208.             }
  209.             }
  210.  
and here is the replace statement:
Expand|Select|Wrap|Line Numbers
  1.     public string cleanText(string c)
  2.     {
  3.         string cleanText = c.Replace("/", "-");
  4.         string cleanText2 = cleanText.Replace("\\", "-");
  5.         string cleanText3 = cleanText2.Replace(":", "-");
  6.         string cleanText4 = cleanText3.Replace("?", "-");
  7.         string cleanText5 = cleanText4.Replace("*", "-");
  8.         string cleanText6 = cleanText5.Replace("|", "-");
  9.         string cleanText7 = cleanText6.Replace("\"", "");
  10.  
  11.  
  12.         return cleanText7;
  13.     }
  14.  
Thanks for everyones help on this.
Jul 27 '10 #1
15 3577
JKing
1,206 Expert 1GB
What is the value of the string newFile?
Jul 28 '10 #2
Fuzz13
110 100+
The user types in a text box a name of a folder (this folder is not one that exists but rather one that will be made)
The name typed in is stored as value "newobject.ParentFolder"
Next the user will browse to an already existing folder from which to work from (i.e. My Documents)
The directory name of the folder they browsed to is stored as "outputdir"
The next step is that the program combines the outputdir (directory name of browsed to folder) with the to-be-created folder "parentfolder"
This combination of "outputdir" and "parentfolder" is then stored as "newPath"
The program then builds the users new folder (the folder for the given project) in this line of code:
Expand|Select|Wrap|Line Numbers
  1. System.IO.Directory.CreateDirectory(newPath);
  2.  
Next there is a for loop that will continue for every item within the given array (the array's length is determined earlier in the code when the file selected is read in line by line)

Then I have each object in the array add the extension ".txt" here:
Expand|Select|Wrap|Line Numbers
  1. string fileName = lines[i] + ".txt";
  2.  
as you can see the new "object[i].txt"
is stored as "fileName" (fileName is reassigned with each iteration of the for loop to accommodate the next object of the array.
This shouldn't be an issue as the file is being created before restarting the beginning of the for loop and reassigning the value "fileName")

Finally there is the newFile value you asked about.
"newFile" is stored as a combination of "newPath" (i.e. combination of "outputdir" and "parentfolder") and "fileName" (i.e. object[i].txt)

So for each "newFile" it should be creating either a file or a folder (based off a selection within the program)
the file/folder will be named based off the name of the currently read object of the array, and created inside the folder name
the user types in to create (a folder not in existence) and that folder will be created within a selected directory (i.e. My Documents)

That was a very long answer to your question but I think it was necessary since newFile is based off a lot of other combinations.

Thanks for your help.
Jul 28 '10 #3
JKing
1,206 Expert 1GB
That still doesn't answer my question. I understand your logic in building the string newFile based off user-input and other variables. That is all fine and dandy but I want to know the value of the string before the error occurs. I am assuming the error is occurring on one of the following lines:
Expand|Select|Wrap|Line Numbers
  1. System.IO.File.Create(@newFile);
If this is the case I want to know the value of newFile when the error occurred. This will help hint as to what is going on.
Jul 28 '10 #4
Fuzz13
110 100+
Ah. I totally misunderstood. The file being loaded appears as such:
Expand|Select|Wrap|Line Numbers
  1. first line is safe
  2. 2nd line has ** illegal char
  3. third line is safe
  4. ** oh no
  5. hello // goodbye
  6. "try this"
  7. \and this?
  8.  
The weird thing is any line by itself is running fine for example:

"This sentence needs fixed***"

the program will either fail my test in place and give an error (one of mine) or if the user chooses to replace illegal characters it will create the file/folder named:

"This sentence needs fixed---"

Hope that was the appropriate answer to your question.
Jul 28 '10 #5
JKing
1,206 Expert 1GB
Okay you are missing what I mean.

Expand|Select|Wrap|Line Numbers
  1.  
  2.  private void Runbtn_Click(object sender, EventArgs e)
  3.  
  4.         {//1
  5.             string[] lines = null; //create lines array
  6.  
  7.             using (StreamReader sr = new StreamReader(@newobject.csvDirectory))
  8.             {//2
  9.                 string fileContents = sr.ReadToEnd();
  10.                 lines = fileContents.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  11.             }//2
  12.  
  13.             bool safeLength = true;
  14.  
  15.             if (Filebtn.Checked == true)
  16.             {//3                
  17.  
  18.                 for (int i = 0; i < lines.Length; i++)
  19.                 {//4
  20.                     if (lines[i].Length > MAXFILENAME)
  21.                     {//5
  22.                         newobject.fileNameError();
  23.                         safeLength = false;
  24.                         break;
  25.                     }//5
  26.  
  27.                 }//4
  28.  
  29.                 if (safeLength == true)
  30.                 {//4
  31.  
  32.                     if (cleancheckbox.Checked == true)
  33.                     { //5                                                           
  34.                         newobject.creationInProgress(); // prompts that files are being made                    
  35.  
  36.                         //create the child form
  37.                         newobject.ParentFolder = newfldrbx.Text;
  38.  
  39.                         // Specify a folder to house everything new inside
  40.                         //This is not the folder that holds the created files/folders
  41.                         // based off of csv/txt values
  42.                         string outputdir = folderBrowserDialog1.SelectedPath;
  43.  
  44.                         //Create a new subfolder under the current active folder
  45.                         // this new folder will hold all files/folders created
  46.                         // per project.
  47.                         string newPath = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  48.  
  49.                         // Create the subfolder
  50.                         //System.IO.Directory.CreateDirectory(newPath);
  51.                         MessageBox.Show(newPath);
  52.  
  53.                         for (int i = 0; i < lines.Length; i++)
  54.                         {//6
  55.                             lines[i] = newobject.cleanText(lines[i]); // sends data to get cleaned
  56.  
  57.  
  58.                             //takes the input from the new folder text box
  59.                             // and assigns it to the filename variable
  60.                             string fileName = lines[i] + ".txt";
  61.  
  62.                             // creates new file based off of file name input annd
  63.                             // parent directory
  64.                             string newFile = System.IO.Path.Combine(newPath, fileName);
  65.                             //System.IO.File.Create(@newFile);
  66.                             MessageBox.Show(newFile);
  67.                         }//6
  68.                     }//5
  69.  
  70.                     else if (cleancheckbox.Checked == false)
  71.                     {
  72.  
  73.                         // Return an error for file with illegal characters
  74.  
  75.                         bool linesOK = true;
  76.                         for (int i = 0; i < lines.Length; i++)
  77.                         {
  78.                             if (lines[i].IndexOfAny(illegal) != -1)
  79.                             {
  80.                                 newobject.illegalError();
  81.                                 linesOK = false;
  82.                                 break;
  83.                             }
  84.                         }
  85.  
  86.                         if (linesOK == true)
  87.                         {
  88.                             newobject.creationInProgress();
  89.  
  90.                             for (int i = 0; i < lines.Length; i++)
  91.                             {
  92.                                 // stores the string entered for the new folder name
  93.                                 newobject.ParentFolder = newfldrbx.Text;
  94.  
  95.                                 // Specify a folder to house everything new inside
  96.                                 //This is not the folder that holds the created files/folders
  97.                                 // based off of csv/txt values
  98.                                 string outputdir = folderBrowserDialog1.SelectedPath;
  99.  
  100.                                 //Create a new subfolder under the current active folder
  101.                                 // this new folder will hold all files/folders created
  102.                                 // per project.
  103.                                 string newPath = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  104.  
  105.                                 // Create the subfolder
  106.                                 //System.IO.Directory.CreateDirectory(newPath);
  107.                                 MessageBox.Show(newPath);
  108.  
  109.                                 //takes the input from the new folder text box
  110.                                 // and assigns it to the filename variable
  111.                                 string fileName = lines[i] + ".txt";
  112.  
  113.                                 // creates new file based off of file name input annd
  114.                                 // parent directory
  115.                                 string newFile = System.IO.Path.Combine(newPath, fileName);
  116.                                 //System.IO.File.Create(@newFile);
  117.                                 MessageBox.Show(newFile);
  118.                             }
  119.                         }
  120.                     }
  121.                 }
  122.             }
  123.  
  124.  
  125.             else if (folderbtn.Checked == true)
  126.             {
  127.                 if (cleancheckbox.Checked == true)
  128.                 {
  129.  
  130.  
  131.                     // Get the value to be passed
  132.                     string parentfolder = newfldrbx.Text;
  133.  
  134.                     //create the child form
  135.                     newobject.ParentFolder = parentfolder;
  136.  
  137.                     // Specify a "currently active folder"
  138.                     string outputdir = folderBrowserDialog1.SelectedPath;
  139.  
  140.                     //Create a new subfolder under the current active folder
  141.                     string newParent = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  142.  
  143.                     // Create the subfolder
  144.                     //System.IO.Directory.CreateDirectory(newParent);
  145.                     MessageBox.Show(newParent);
  146.  
  147.  
  148.                     for (int i = 0; i < lines.Length; i++)
  149.                     {
  150.                         lines[i] = newobject.cleanText(lines[i]);
  151.  
  152.                         //Create a new subfolder under the current active folder
  153.                         string newChild = System.IO.Path.Combine(newParent, lines[i]);
  154.  
  155.                         // Create the subfolder
  156.                         //System.IO.Directory.CreateDirectory(newChild);
  157.                         MessageBox.Show(newChild);
  158.                     }
  159.  
  160.                     newobject.creationInProgress();
  161.                 }
  162.  
  163.                 else if (cleancheckbox.Checked == false)
  164.                 {
  165.  
  166.  
  167.                     // Return an error for file with illegal characters
  168.                     bool linesOK = true;
  169.                     for (int i = 0; i < lines.Length; i++)
  170.                     {
  171.                         if (lines[i].IndexOfAny(illegal) != -1)
  172.                         {
  173.                             newobject.illegalError();
  174.                             linesOK = false;
  175.                             break;
  176.                         }
  177.                     }
  178.  
  179.                     // if the file chosen has no illegal characers this this set of
  180.                     // commmands occurs
  181.                     if (linesOK == true)
  182.                     {
  183.  
  184.                         // Get the value to be passed
  185.                         string parentfolder = newfldrbx.Text;
  186.  
  187.                         //create the child form
  188.                         newobject.ParentFolder = parentfolder;
  189.  
  190.                         // Specify a "currently active folder"
  191.                         string outputdir = folderBrowserDialog1.SelectedPath;
  192.  
  193.                         //Create a new subfolder under the current active folder
  194.                         string newParent = System.IO.Path.Combine(outputdir, newobject.ParentFolder);
  195.  
  196.                         // Create the subfolder
  197.                         //System.IO.Directory.CreateDirectory(newParent);
  198.                         MessageBox.Show(newParent);
  199.  
  200.                         for (int k = 0; k < lines.Length; k++)
  201.                         {
  202.                             //Create a new subfolder under the current active folder
  203.                             string newChild = System.IO.Path.Combine(newParent, lines[k]);
  204.  
  205.                             // Create the subfolder
  206.                             //System.IO.Directory.CreateDirectory(newChild);
  207.                             MessageBox.Show(newChild);
  208.  
  209.                         }
  210.                         // this lets the user know the folders were made
  211.                         // and must lie outside of the loops to only iterate
  212.                         // one time
  213.                         newobject.creationInProgress();
  214.  
  215.                     }
  216.                 }
  217.             }
  218.             }
  219.  
  220.  
Copy your current code and paste it into a text file and save it for a backup.
Now copy this code paste it in and run your program. Enter some values for the folder names and tell me what the messageboxes say.
Jul 28 '10 #6
JKing
1,206 Expert 1GB
What I am trying to get you to see is that your variable newFile and/or newPath may not contain the data that you think they do...

I don't have access to VS at the moment but I am fairly certain that the file dialog selected path returns the path with a \ at the end. Use that with the Path.Combine method and you now have a \\ in your string. The @ symbol on newFile says to take it literally and this will give you your illegal character error.
Jul 28 '10 #7
Fuzz13
110 100+
I'm getting the same error as before: Illegal character

Here's the details:
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text **************
System.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path)
at System.IO.Path.Combine(String path1, String path2)
at WindowsFormsApplication1.Form1.Runbtn_Click(Object sender, EventArgs e) in C:\Users\Dahlia\Desktop\CSV program\Form1.cs:line 137
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnMouseUp(MouseEventAr gs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.O nMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.W ndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)


************** Loaded Assemblies **************
mscorlib
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 (RTMRel.030319-0100)
CodeBase: file:///C:/Windows/Microsoft.NET/Framework/v4.0.30319/mscorlib.dll
----------------------------------------
WindowsFormsApplication1
Assembly Version: 1.0.0.0
Win32 Version: 1.0.0.0
CodeBase: file:///C:/Users/Dahlia/Desktop/CSV%20program/WindowsFormsApplication1/WindowsFormsApplication1/bin/Release/WindowsFormsApplication1.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Windows.Forms/v4.0_4.0.0.0__b77a5c561934e089/System.Windows.Forms.dll
----------------------------------------
System.Drawing
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Drawing/v4.0_4.0.0.0__b03f5f7f11d50a3a/System.Drawing.dll
----------------------------------------
System
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System/v4.0_4.0.0.0__b77a5c561934e089/System.dll
----------------------------------------
System.Xml
Assembly Version: 4.0.0.0
Win32 Version: 4.0.30319.1 built by: RTMRel
CodeBase: file:///C:/Windows/Microsoft.Net/assembly/GAC_MSIL/System.Xml/v4.0_4.0.0.0__b77a5c561934e089/System.Xml.dll
----------------------------------------

************** JIT Debugging **************
To enable just-in-time (JIT) debugging, the .config file for this
application or computer (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.

For example:

<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>

When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the computer
rather than be handled by this dialog box.
Jul 29 '10 #8
JKing
1,206 Expert 1GB
Your string variable outputdir ends with a \, when you try to use it in the Path.Combine method it throws the illegal character error.
Jul 29 '10 #9
Fuzz13
110 100+
If the outputdir ends with a \ wouldn't it always give me an illegal character error then? I can get the program to run if it is loading a file with only a single line of text.
Jul 29 '10 #10
Fuzz13
110 100+
My code populates a corresponding text box with the directory stored as "outputdir" and it does not end with a "\" so I think the problem lies elsewhere.
Jul 29 '10 #11
JKing
1,206 Expert 1GB
@Fuzz13
Do that with your other variables and you should be able to pin point the error.
Jul 29 '10 #12
Fuzz13
110 100+
I am pretty sure I have found the line of code that is giving me the error.

Expand|Select|Wrap|Line Numbers
  1.                 string fileName = lines[i] + ".txt";
  2.  
  3.                 string newFile = System.IO.Path.Combine(newPath, fileName);
  4.  

The presence of "lines[i]" is the issue. If I instead use a string literal like this:

Expand|Select|Wrap|Line Numbers
  1.                 string fileName = "something nice" + ".txt";
  2.  
  3.                 string newFile = System.IO.Path.Combine(newPath, fileName);
  4.  
It will work just fine. The debugger says the error is in the second line of code where the two are combined but it's dependent on that "lines[i]" bit. It also crashes if I use "lines[0]" or any given value. That narrows it down but I don't know what to do about it. Any suggestions?
Jul 29 '10 #13
JKing
1,206 Expert 1GB
Check the value of lines[i] and verify that there are no illegal characters in it.
Jul 29 '10 #14
Fuzz13
110 100+
I have. There are none. The value of lines[i] appears as:

"This is text"
"This is more text"
"Again more text"

(the quotes are not part of the actual text)
Jul 29 '10 #15
Fuzz13
110 100+
Ok I got it to run using the lines[i] code if there is only 1 line of data in the file loading. Multiple lines of data returns an Error. I am wondering if the trouble its having is coming from how the lines of data are being read into the array:

Expand|Select|Wrap|Line Numbers
  1. string[] lines = null; //create lines array
  2.  
  3.             using (StreamReader sr = new StreamReader(@newobject.csvDirectory))
  4.             {
  5.                 string fileContents = sr.ReadToEnd();
  6.                 lines = fileContents.Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  7.             }
  8.  
This at one point had worked properly but something changed in my code and it no longer does so I'm not sure what it is. The code posted should read each line of the file into an array as an object. (line 1 of file = object 0).

Thoughts?
Jul 30 '10 #16

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: Sean Bartholomew | last post by:
i have a bit of a problem. im parsing a record string using strtok but im encountering back to back whitespaces (\t\t) due to empty fields from my database export. the STRTOK function reads up to...
16
by: drj0nson | last post by:
What is the right way of creating a string/char array and assigning to a char* which is then used in a function call. Thought it would be quite nice to avoid a memory leakage /core dump. 1...
2
by: Alvin Bruney | last post by:
Anybody know how to fix this. Currently only a reboot will cure it. I'm sick and fed up of rebooting. It has to be one of these services running in the background doing this. It can happen with any...
12
by: mast2as | last post by:
Hi everyone... I have a TExceptionHandler class that is uses in the code to thow exceptions. Whenever an exception is thrown the TExceptionHander constructor takes an error code (int) as an...
2
by: dasilva109 | last post by:
Hi guys I am new to C++ and need urgent help with this part of my code for a uni coursework I have to submit by Thursday //ClientData.h #ifndef CLIENTDATA_H #define CLIENTDATA_H #include...
0
by: devmentee | last post by:
Hi All, I am compiling a VS2003 mixed mode C++ DLL under VS2005 and I get the following linker error. I am using STL map inside a C++ class which is compiled as a mixed mode DLL. I have...
21
by: one2001boy | last post by:
PostMessage() function returns ERROR_NOT_ENOUGH_QUOTA after running in a loop for 700 times, but the disk space and memory are still big enough. any suggestion to resolve this problem? thanks.
3
by: Tarik Monem | last post by:
Hi Everyone, Still a newbie with FLEX, and I've passed arrays using AJAX to FLEX before, but I've never passed links to FLEX. Basically, this is the OUTPUT, which I wanted, but I'm given an...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
13
by: Hongyu | last post by:
Hi, I have a datetime char string returned from ctime_r, and it is in the format like ""Wed Jun 30 21:49:08 1993\n\0", which has 26 chars including the last terminate char '\0', and i would...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.