special characters in system filenames

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Art

    special characters in system filenames

    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.Url Encode(...) 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
  • =?ISO-8859-1?Q?Arne_Vajh=F8j?=

    #2
    Re: special characters in system filenames

    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.Url Encode(...) 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(r es, @"[^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

    Comment

    • Peter Macej

      #3
      Re: special characters in system filenames

      If the file name doesn't have to be human readable, you can use base64
      encoding. It's one line solution, just use Convert.ToBase6 4String method.

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

      Comment

      Working...