special characters in system filenames

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
 
A

Arne Vajhøj

Art said:
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.Groups[0].Value, "__" +
((int)reg.Groups[0].Value[0]).ToString("X2"));
}
return res;
}

Arne
 
P

Peter Macej

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.
 

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

Top