Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8396031
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:26:30+00:00 2026-06-09T20:26:30+00:00

My paths are UTF-16 strings. Most of them only use the ASCII set, so

  • 0

My paths are UTF-16 strings. Most of them only use the ASCII set, so a filename like test would be stored as

T \x00 E \x00 S \x00 T \x00

I use Encoding.Unicode.GetString(bytes) to read the string and it works fine (when I print them out to console or to a form control it appears as I expect), but when I want to actually create a file with the given filename using the following code

BinaryWriter outFile = new BinaryWriter(File.OpenWrite(path));

I get an exception

Unhandled Exception: System.ArgumentException: Illegal characters in path.
   at System.IO.Path.CheckInvalidPathChars(String path)
   at System.IO.Path.GetFileName(String path)

It’s probably because there are null chars in there (maybe it stores the original byte array internally), but I’m not sure how to deal with it. Not all of the strings are ASCII though, and some of the characters use two-bytes.

UPDATE:

Turns out the illegal bytes were simply null bytes that were padded to the string. However, I can’t simply strip all trailing null bytes, but I don’t know the length of the string either. How would I strip null bytes from a string where each character is stored in n bytes?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T20:26:32+00:00Added an answer on June 9, 2026 at 8:26 pm

    From the MSDN on `Path.GetInvalidPathChars’

    The full set of invalid characters can vary by file system. For
    example, on Windows-based desktop platforms, invalid path characters
    might include ASCII/Unicode characters 1 through 31, as well as quote
    (“), less than (<), greater than (>), pipe (|), backspace (\b), null
    (\0) and tab (\t).

    You could use Path.GetInvalidPathChars as a filter. Copy your input string to an output string while filtering any characters that match a character from Path.CheckInvalidPathChars.

    Here’s an example I cooked up:

    string input = @"This <path> ""contains"" |some| ~invalid~ characters";
    
    var invalidChars = Path.GetInvalidPathChars();
    
    string output = input.Aggregate(new StringBuilder(), (sb, c) => invalidChars.Contains(c) ? sb : sb.Append(c), sb => sb.ToString());
    
    // output contains: This path contains some ~invalid~ characters
    

    Note that most symbols are filtered out, but the tildes are not, since they are valid path characters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to store all paths to headers in separate file. I'm going
I'm trying to load a bunch of utf-8 encoded strings and dump them again
I have a set of data, but I need to work only with utf-8
Is it possible to use Windows API ANSI functions with UTF-8 strings? For example,
I would like to read an XML file and modify some strings, save then
So I rewrote my paths to something like: URL/really/nice/paths/ using mod_rewrite rules like this:
I would like some advice. I have encountered the following error in Python 2.6:
I use a Localizable.strings file and replaces the strings in my app with NSLocalizedString(@KEY,@
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; NSString *myPlistPath =
I have often PATHs for files which do not exist in my codes. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.