a space in FILE PATH gives a "Illegal characters in path" exception, otherwise works fine

Last post 01-26-2007 2:42 PM by pickyh3d. 4 replies.

Sort Posts:

  • a space in FILE PATH gives a "Illegal characters in path" exception, otherwise works fine

    11-08-2006, 12:15 PM
    • Member
      10 point Member
    • enzocontini
    • Member since 11-08-2006, 5:04 PM
    • Posts 2

    How can I avoid that error if the path contains a directory/filename with spaces?

    FileStream inputStream = new FileStream("C:\\provapippo.jpg", FileMode.Open, FileAccess.Read); // WORKS 

    FileStream

    inputStream = new FileStream("C:\\prova pippo.jpg", FileMode.Open, FileAccess.Read); //EXCEPTION because of the space in the filename

     OR

    Bitmap photo = new Bitmap("c:\\provapippo.jpg"); // WORKS 

    Bitmap

    photo =
    new Bitmap("c:\\prova pippo.jpg);  //EXCEPTION because of the space in the filename

    I have already tried to add a " at the bebinning and at the end of the path string (that is "\"C:\\prova pippo.jpg\"" )... but nothing ...

    Let me know

    Enzo

  • Re: a space in FILE PATH gives a "Illegal characters in path" exception, otherwise works fine

    11-08-2006, 1:56 PM
    • All-Star
      24,346 point All-Star
    • budugu
    • Member since 01-12-2006, 2:15 PM
    • North Carolina
    • Posts 3,651

    Try ..

    @"C:\\prova pippo.jpg"

    Vijay Kodali || My Blog


    "Don't be afraid to be wrong; otherwise you'll never be right."
  • Re: a space in FILE PATH gives a "Illegal characters in path" exception, otherwise works fine

    11-09-2006, 3:28 AM
    • Member
      10 point Member
    • enzocontini
    • Member since 11-08-2006, 5:04 PM
    • Posts 2

    Thanks for your answer!

    Today I tryed to debug again that code and .... it works (also without using @): the only thing I can suppose is that there was something wrong with the debugging environment (today the code is the same of yesterday!) ... or I was too tired and I did some mistake!!

    Thank ypou again indeed: anyway I think that if one use @  the \ must not be duplicated  (so @"C:\prova pippo.jpg")

     

  • Re: a space in FILE PATH gives a "Illegal characters in path" exception, otherwise works fine

    01-26-2007, 8:16 AM
    • Member
      2 point Member
    • honda
    • Member since 11-15-2006, 1:52 PM
    • Posts 1

    Hello i use the @ and then it works.

    But now a collega ask me what does the @ and my answers is ???.

    Can someone help me what @ does with your string??

     Greetings Henk

  • Re: a space in FILE PATH gives a "Illegal characters in path" exception, otherwise works fine

    01-26-2007, 2:42 PM
    • Star
      9,676 point Star
    • pickyh3d
    • Member since 09-17-2002, 6:19 AM
    • Virginia
    • Posts 1,955

    In C#, the @ symbol tells the compiler to take what's inside the string literally.

    For example: 

    // without the literal
    string NotLiteral = "This string has to escape \"special characters,\" such as\r\n\\, \\r, \\n, \\t, \\\", \\\'\r\nin order to be understood by the compiler.";
    // with the literal
    string Literal = @"This string has to escape ""special characters,"" such as
    \, \r, \n, \t, \"", \'
    in order to be understood by the compiler.";
    
    // true
    Response.WriteLine(NotLiteral == Literal);
    Response.WriteLine(Literal);
    // displays:
    /*
    True
    This string has to escape "special characters," such as
    \, \r, \n, \t, \", \'
    in order to be understood by the compiler.
    */

    Now, one thing to note is that literal strings still must escape double quotes, but now you escape them just like in VB.NET, by adding a second double quote ("" for one " inside of a literal string) instead of doing \". That is just a limitation of using double quotes to start and end a string, so there is nothing they could do to avoid that escape requirement.

    Note that the literal string reads in the new lines (\r\n) that you entered by going to the next line without having to put them in manually and that the string itself spans 3 lines. The non-literal string could be broken into multiple strings to do this for clarity, such as (without a performance hit because the compiler should recognize 3 constant strings being concatenated and do that for us):

    // without literal
    string NotLiteral = "This string has to escape \"special characters,\" such as" + 
                        "\r\n\\, \\r, \\n, \\t, \\\", \\\'" +
                        "\r\nin order to be understood by the compiler.";

    Hopefully this was clear and not as jumbled as I am starting to think that it might be.

    Picky
Page 1 of 1 (5 items)