I wrote this function about a year ago, it's very useful for generating safe file or folder names from text that a user inputs.

One method I use this for is storing the filename in the database and then url-rewriting with global.asax. You can get an example of url rewriting here.

public static string FilenameFromTitle(string name)
    // first trim the raw string
    string safe = name.Trim();

    // replace spaces with hyphens
    safe = safe.Replace(" ", "-").ToLower();

    // replace any 'double spaces' with singles
    if(safe.IndexOf("--") > -1)
        while(safe.IndexOf("--") > -1)
            safe = safe.Replace("--", "-");

    // trim out illegal characters
    safe = Regex.Replace(safe, "[^a-z0-9\\-]", "");

    // trim the length
    if(safe.Length > 50)
        safe = safe.Substring(0, 49);

    // clean the beginning and end of the filename
    char[] replace = {'-','.'};
    safe = safe.TrimStart(replace);
    safe = safe.TrimEnd(replace);

    return safe;
10 Years
Discussion Span
Last Post by Momerath
foreach ( char lDisallowed in System.IO.Path.GetInvalidFileNameChars( ) ) {
  safe = safe.Replace( lDisallowed.ToString(), "" ); 
foreach ( char lDisallowed in System.IO.Path.GetInvalidPathChars( ) ) {
  safe = safe.Replace( lDisallowed.ToString(), "" ); 

I needed this is VB.net so I did a little translation, here is the VB version:

Public Function SafeFileName(ByVal nom As String) As String

        'First Trim the raw string
        Dim safe As String = nom.Trim

        'Replace spaces with hyphens
        safe = safe.Replace(" ", "-").ToLower()

        'Replace double spaces with singles
        If safe.IndexOf("--") > 1 Then
            While (safe.IndexOf("--") > -1)
                safe = safe.Replace("--", "-")
            End While
        End If

        'Trim out illegal characters
        safe = regex.replace(safe, "[^a-z0-9\\-]", "")

        'Trim Length
        If safe.Length > 50 Then
            safe = safe.Substring(0, 49)
        End If

        'Clean the beginning and end of filename
        Dim replace As Char() = {"-", "."}
        safe = safe.TrimStart(replace)
        safe = safe.TrimEnd(replace)

        Return safe

    End Function

Edited by jangeador: n/a


can you write me why you used two back slashes in this statement.

safe = regex.replace(safe, "[^a-z0-9\\-]", "")


can you write me why you used two back slashes in this statement.

safe = regex.replace(safe, "[^a-z0-9\\-]", "")

Backslash is used as an 'escape' character for when you want to indicate something special. For example '\w' means 'word' in a regex and is a shorthand way of saying "anything made of characters'. In this case '\\' means that I want the escape character itself, the backslash, to be one of the entries in this matching pattern.

I'd also like to point out that this is a five year old thread, and the original author hasn't logged on for almost five years.

Edited by Momerath: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.