I want to upload a file into the database. While uploading I'm extracting the file name and extension. The problem now is if I have a file name which is separated by '_', it is giving me and exception "illegal characters in path "

string text = File.ReadAllText("C:\\Users\\xxxx\\Desktop\\9_file.bak", Encoding.GetEncoding(437));
byte[] data = File.ReadAllBytes(text);
filename = Path.GetFileNameWithoutExtension(text);
extension = Path.GetExtension(text);
OdbcCommand Odbc2 = new OdbcCommand();
Odbc2.CommandText = "insert INTO table ( blob) VALUES (?)";
Odbc2.Parameters.AddWithValue("@blob", data);
Odbc2.Connection = OdbcCon;

Please help me to by pass the error.


There is nothing wrong with using an underscore in a file name.
If you take it out, does the problem go away?

Does the x'd out user folder have any non-alphanumeric or non-whitespace characters?

I agree with nmaillet.
I just tried this and it worked even with the special characters:

string strAll = File.ReadAllText("c:\\science\\9_test's`_~out.txt", System.Text.Encoding.GetEncoding(437));

Characters allowed
Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

  • The following reserved characters:
    • < (less than)
    • > (greater than)
    • : (colon)
    • " (double quote)
    • / (forward slash)
    • \ (backslash)
    • | (vertical bar or pipe)
    • ? (question mark)
    • * (asterisk)
  • Integer value zero, sometimes referred to as the ASCII NUL character.
  • Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.
  • Any other character that the target file system does not allow.

It would be odd for the user folder to have an illegal path name, as it would be an existing path, therefore, allowed by the operating system...

EDIT: Unless it's been typo'd of course ^^

EDIT2: Check for illegal characters in the text you're pulling back from the file. I actually imagine that's where the mistake is being made. You possibly have a linefeed or carriage return character in there.