CSharpTest.Net
SafeFileName Method
See Also  Example Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Utils Namespace > StringUtils Class : SafeFileName Method

name
The text to parse

Glossary Item Box

Parses the text to ensure that it is a valid file name and returns the resulting string with the following offending characters replace with '-': /\:*?"'<>| Also removes any characters class as control characters, or anything below 32 space, this would include tab, backspace, newline, linefeed, etc. If provided null, this function returns null

Syntax

Visual Basic (Declaration) 
Public Shared Function SafeFileName( _
   ByVal name As String _
) As String
C# 
public static string SafeFileName( 
   string name
)

Parameters

name
The text to parse

Return Value

The text provided with only allowable characters

Example

Library/Library.Test/TestStringUtils.cs

C#Copy Code
Assert.AreEqual(null, StringUtils.SafeFileName(null));
Assert.AreEqual("--------------", StringUtils.SafeFileName("\b\t\r\n/\\:*?\"'<>|"));

Assert.AreEqual("Greetings this is !@#)%~^@+)(",
    StringUtils.SafeFileName("Greetings this is !@#)%~^@+)("));

Assert.AreEqual("-ca761232-ed42-11ce-bacd-00aa0057b223-",
    StringUtils.SafeFileName("<ca761232|ed42|11ce|bacd|00aa0057b223>"));
VB.NETCopy Code
Assert.AreEqual(Nothing, StringUtils.SafeFileName(Nothing))
Assert.AreEqual("--------------", StringUtils.SafeFileName(vbBack & vbTab & vbCr & vbLf & "/\:*?""'<>|"))

Assert.AreEqual("Greetings this is !@#)%~^@+)(", StringUtils.SafeFileName("Greetings this is !@#)%~^@+)("))

Assert.AreEqual("-ca761232-ed42-11ce-bacd-00aa0057b223-", StringUtils.SafeFileName("<ca761232|ed42|11ce|bacd|00aa0057b223>"))

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys