locked
Error: "The path is not of a legal form" in C# RRS feed

  • Question

  •   Hello.I receive the following error Error: "The path is not of a legal form" in C# in the code

     

          

    string location = "List.txt";

    // Read lines

    StreamReader reader = new StreamReader(location);

    string[] file_lines = (reader.ReadToEnd()).Split('\n');

    // Add in Lista

    foreach (string one_line in file_lines)

    string file_location = one_line.TrimEnd();

    System.IO.FileInfo info_item = new FileInfo(file_location);

    if (info_item.Extension == ".mp3")

    {

    ListViewItem List_Item = new ListViewItem(info_item.Name, 0);

    List_Item.SubItems.Add("XX:XX");

    List_Item.SubItems.Add("Necunoscut");

    List_Item.SubItems.Add(locatie_fisier_lista);

    Box_Lista_Melodii.Items.AddRange(new ListViewItem[] { List_Item });

    }

    }

     

     

    Saturday, July 1, 2006 11:12 AM

Answers

  • After trimming the string make sure it's length is greater than zero.  :)
    Monday, July 3, 2006 12:14 PM
  • in your foreach loop check the length of the line first before proceeding to do the code:

     



    foreach (string curLine in theLinesInFile)
    {
       if (curLine.Length > 0)
       {
          //do stuff
       }
    }

     

    Monday, July 3, 2006 12:23 PM

All replies

  • To me it looks like the current line has some invalid path characters or something as such.

    Are you able to get a list of each file it is reading and display it here for our and your benefit? This is so we can see what line the error is happening on and to see what entries are in the text file/lines array

     

    Sunday, July 2, 2006 12:50 PM
  • Well.For the moment I will explain you:
                -     I have a LISTVIEW with 3 columns (Name, Duration, Path).Is like a playlist.When the user double-click a item, will be played the information from Path (C:\Music.mp3). THIS IS WORKING VERY GOOD
                -    When the application is closing, it saves the Path in a textfile, line per line , like this:

    using (StreamWriter sw = new StreamWriter("List.txt"))
                {
                    foreach (ListViewItem item_for_saving in Box_Lista_Melodii.Items)
                    {
                        if (item_for_saving.SubItems.Count >= 4)
                        {
                            string item_name = item_for_saving.SubItems[2].Text;
                            sw.WriteLine(item_name);
                        }
                        else
                        {

                        }
                    }
     OK THIS IS WORKING VERY GOOD.IT SAVES SOMETHING LIKE THIS:
    C:/Music.mp3

      When the application is starting it must add the item from LIST.TXT in LISTVIEW
                    - it gets a line (that will be the PATH) and then with System.IO.File(PATH) must get information about file (Name, and Duration) >> this is NOT WORKING
    WHY?
              
    Sunday, July 2, 2006 1:14 PM
  • Hi

    As suggested, can you please write here what each line is currently reading in the foreach loop and also a sample from the list.txt?

     

    As well as this, by looking at your example - you have said that "it saves something like this : C:/music.mp3"

     

    that will of course through an exception as the path is an illegal path for the class to work with. it should be a backslash '\' rather than a forward '/'

     

    Make sure that initially when you are reading the file paths to the listbox that it displays the full path to the file in a correct format, ie:

    c:\folder\mymusic.mp3

    Sunday, July 2, 2006 2:08 PM
  • Well.For example the first 2 lines:
    F:\My Music\Music 2006\Andreea Banica - Indragostiti - www.HOmp3.Tk -.mp3
    F:\My Music\Music 2006\HI-Q Razna!.mp3


    The problem is that the program itself saves the path when the user close the application, but when the application is starting he is not recognize the line.
     
    Sunday, July 2, 2006 2:28 PM
  • well the path/file you have given an example of seems to be ok.

    As you have said, when the application is closed the application writes the files back to the text file - correct?

    The first 2 lines which you have posted - are these of the file itself or what is going through the foreach loop?

     

    If you can, please report back on this:

    run the application

    save the listbox items (Containing the path and filename)

    open the textfile, does the path look correct? (post a single line)

    run the application again, make it read the textfile - does it read it? (assuming the path looks correct)

    at what part/how far in the foreach loop does it throw the exception? what was the value of the variable in the foreach loop that caused the exception to be thrown?

     

    it could also be that there maybe an entry somewhere which is an invalid entry causing it to throw the exception.

    Sunday, July 2, 2006 2:51 PM
  •   I try to verify the application

     1 - When I close the program all the things seem to be OK in LIST.TXT

     2 - I replace SystemInfo(line) with MessageBox.Show(line) to see what is containing that line, and all seem to be OK.

     3 - I tried with Trim(),TrimEnd(),TrimStart() and is not working.Error: " The path is not of a legal form"

    Monday, July 3, 2006 8:13 AM
  • Are there any blank lines in the input file (and is there an extra blank line at the end)?
    That would result in this problem.

    Monday, July 3, 2006 8:38 AM
  • Yes.It is at the end of the file......         :( .What I can do?
    Monday, July 3, 2006 12:12 PM
  • After trimming the string make sure it's length is greater than zero.  :)
    Monday, July 3, 2006 12:14 PM
  • How? ;)
    Monday, July 3, 2006 12:20 PM
  • in your foreach loop check the length of the line first before proceeding to do the code:

     



    foreach (string curLine in theLinesInFile)
    {
       if (curLine.Length > 0)
       {
          //do stuff
       }
    }

     

    Monday, July 3, 2006 12:23 PM
  • Well.This time is not adding any item from LIST.TXT.The LISTVIEW is empty
    Monday, July 3, 2006 1:09 PM
  • Ok. I put TrimEnd() and it working.THANKS A LOT....!!!!!!!!!!!!!!!!!!!!!!!
    Monday, July 3, 2006 1:12 PM
  • "The path is not of a legal form" is usually caused when the entire project needs to be rebuilt.
    Friday, November 9, 2007 5:01 PM