By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,376 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

special characters in system filenames

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.