Path.InvalidPathChars question

  • Thread starter Thread starter schaf
  • Start date Start date
S

schaf

Hi !
I have determined, that Path.InvalidPathChars does not return an array
with all invalid path characters. For instance the question mark (?)
does not appear in the array, but it is not possible to create a folder
with a question mark (?).

How can I get the whole range of invalid path characters ? Or is it the
best way, to create a folder and using try/catch ?

Regards
 
You should try catch it as it isnt garunteed to work even with a good name as
permissions or name clashes could still get in the way. Plus it may change in
future OS's. Spaces didnt used to be allowed in windows.

Ciaran O'Donnell
 
schaf said:
Hi !
I have determined, that Path.InvalidPathChars does not return an array
with all invalid path characters. For instance the question mark (?)
does not appear in the array, but it is not possible to create a folder
with a question mark (?).

Hi,
MSDN: This array (InvalidPathChars) is not guaranteed to contain the
complete set of characters that are invalid in file and directory
names. The full set of invalid characters can vary by file system...
How can I get the whole range of invalid path characters ? Or is it the
best way, to create a folder and using try/catch ?

Regards

Alternative way is to leave in the path name only exactly valid
characters instead of looking for possible invalid ones.
Ex.:
string path = @"c:\\test\df ?sdg\1.txt";
System.Text.RegularExpressions.Regex re = new
System.Text.RegularExpressions.Regex(@"[^\w\\:.]+");
path = re.Replace(path, "");
Maybe it helps somehow.
 
Hi marss
Alternative way is to leave in the path name only exactly valid
characters instead of looking for possible invalid ones.
Ex.:
string path = @"c:\\test\df ?sdg\1.txt";
System.Text.RegularExpressions.Regex re = new
System.Text.RegularExpressions.Regex(@"[^\w\\:.]+");
path = re.Replace(path, "");
Maybe it helps somehow.

I have a similar alternative way, I do not want to update my regex or
char-array, each time the user changes the OS. And I'm not sure, if the
chinese version of windows has the same invalid character like a german
or english windows.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top