467,889 Members | 1,400 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,889 developers. It's quick & easy.

special characters in system filenames

Art
hey, i think this must be really easy:

i want to use strings as filenames that include characters that are
not allowed in windows filenames. is there a special converter method
for this? something like HttpUtility.UrlEncode(...) for Urls.

if not, what characters do i have to replace? is there an encoding
that is valid? i could create a byte[] from the utf8-string and encode
it into a <whatever encoding is valid for windows filenames>-string.

thanks in advance,
art
Jun 27 '08 #1
  • viewed: 2200
Share:
2 Replies
Art wrote:
i want to use strings as filenames that include characters that are
not allowed in windows filenames. is there a special converter method
for this? something like HttpUtility.UrlEncode(...) for Urls.

if not, what characters do i have to replace? is there an encoding
that is valid? i could create a byte[] from the utf8-string and encode
it into a <whatever encoding is valid for windows filenames>-string.
I am not aware of anything particular.

You can make something up.

One suggestion:

public static string SafeName(string fnm)
{
string res = fnm;
MatchCollection reg = Regex.Matches(res, @"[^A-Za-z0-9-_\.]");
for(int i = 0; i < reg.Count; i++) {
res = res.Replace(reg[i].Groups[0].Value, "__" +
((int)reg[i].Groups[0].Value[0]).ToString("X2"));
}
return res;
}

Arne
Jun 27 '08 #2
If the file name doesn't have to be human readable, you can use base64
encoding. It's one line solution, just use Convert.ToBase64String method.

--
Peter Macej
Helixoft - http://www.helixoft.com
VSdocman - Commenter and generator of class documentation for C#, VB
..NET and ASP .NET code
Jun 27 '08 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Roy W. Andersen | last post: by
3 posts views Thread by Barry Olly | last post: by
11 posts views Thread by Marian Aldenhövel | last post: by
3 posts views Thread by Scott_Tuttle | last post: by
reply views Thread by news.online.de | last post: by
16 posts views Thread by Hugh Janus | last post: by
7 posts views Thread by Trac Bannon | last post: by
reply views Thread by MrMoon | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.