Illegal characters
 CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

1. Member
Join Date
Nov 2011
Posts
37

## 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. ## 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:

: ? * " < > |

3. Member
Join Date
Nov 2011
Posts
37

## 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. ## Re: Illegal characters

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

5. Member
Join Date
Nov 2011
Posts
37

## 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. ## 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).

#### Posting Permissions

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