I have an application that exports the result to a .csv file.
The export takes several project names, one of those projects has the name tag in HTML <b>
.
When trying to create this file, I get an error Illegal character in path.
I have tried to put in the result double quotes:
"" + ProjectTitle + ""
But the error keeps appearing.
How could I avoid getting this error and continue executing my program?
Thank you very much in advance.
Using Linq, you can clean up all illegal characters with a single line of code:
The file names have a series of characters that are not allowed. including the major and minor symbols that are used for redirection.
I would use Path.GetInvalidFileNameChars to clean up disallowed characters from the project name. Something like that:
The error is generated because there are too many characters in filenames that are not allowed for fileName. That is why the file names in operating systems such as windows** only have characters AZ, az, 0-9, spaces and in cases of not applying spaces they use underscore "_", or simple hyphen "-"
You can try a slightly simpler solution than the one posted above.
string fileName = "ProjectTitle"; foreach (char c in System.IO.Path.GetInvalidFileNameChars()) { fileName = fileName.Replace(c, '_'); }
This simple loop finds invalid characters in the filename and replaces them with "_"
I recommend creating the file paths with the Combine method instead of concatenating strings:
Combine method
On this page you can see more methods for handling file names and paths:
Dotnet Perls Path