Illegal characters
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6

Thread: Illegal characters

  1. #1
    Join Date
    Nov 2011
    Posts
    38

    Illegal characters

    I have a text file that contains settings, such as the output file location. Then I call the file in my program...

    File.AppendAllText(@PIoutput, "5," + time.Year + "," + time.DayOfYear + "," + time.ToString("HHmm").TrimStar('0') + "," + time.Second);

    When the program runs, I get the error "Illegal characters in path."

  2. #2
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,005

    Re: Illegal characters

    You don't need to use the string literal operator (@) on a variable. Remove it and see if that works. Usually you would see it's use only when you are inputting a new string that you don't want escaped e.g. string blah = @"this\string\shouldn't\be\escaped";

    Failing that, what is the value of PIoutput? Certain characters are not allowed in file paths such as any of:

    : ? * " < > |
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  3. #3
    Join Date
    Nov 2011
    Posts
    38

    Re: Illegal characters

    I took the "@" out and it still did not work. My output file is defined in the text file as.... string.Format("{0}{1}Metdata_MT-{2:yyyy-MM-dd}.txt", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.DirectorySeparatorChar, DateTime.Now)

    Per what you said, I am assuming this is what is causing the error. lol. But how do I set the output file to this format?

  4. #4
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,005

    Re: Illegal characters

    Hrm. I see nothing obviously wrong with that. What value does PIoutput have in the debugger?
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Nov 2011
    Posts
    38

    Re: Illegal characters

    Ok in my text file, I put the file location. In my program, I called it with

    "output=line + string.Format("{0}{1}Metdata_MT-{2:yyyy-MM-dd}.txt");

    I get the error message FormatException was unhandled. Whats wrong here?

  6. #6
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,005

    Re: Illegal characters

    It's because you use string.Format with {#}'s in the string and then do not provide them as arguments.

    Go back to your original example and set

    Code:
    string PIOutput = string.Format("{0}{1}Metdata_MT-{2:yyyy-MM-dd}.txt", Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), Path.DirectorySeparatorChar, DateTime.Now)
    Then use the debugger to see the value assigned to PIOutput and post it here (or examine it and figure out why it's an invalid path).
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center