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

path
The text to parse

Glossary Item Box

Splits the string on path characters ('/' and '\\') and passes each to SafeFileName(), then reconstructs the string using '\\' and removing any empty segments. If provided null, this function returns null, provided an empty string or just a path seperator '/' it will return String.Empty

Syntax

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

Parameters

path
The text to parse

Return Value

The text provided as a valid path

Example

Library/Library.Test/TestStringUtils.cs

C#Copy Code
Assert.AreEqual(String.Empty, StringUtils.SafeFilePath("\\"));

Assert.AreEqual("----\\--------", StringUtils.SafeFilePath("\b\t\r\n/\\:*?\"'<>|"));

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

Assert.AreEqual("ca761232\\ed42-11ce-bacd\\00aa0057b223",
    StringUtils.SafeFilePath("/ca761232/ed42|11ce|bacd\\00aa0057b223\\"));
VB.NETCopy Code
Assert.AreEqual([String].Empty, StringUtils.SafeFilePath("\"))

Assert.AreEqual("----\--------", StringUtils.SafeFilePath(vbBack & vbTab & vbCr & vbLf & "/\:*?""'<>|"))

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

Assert.AreEqual("ca761232\ed42-11ce-bacd\00aa0057b223", StringUtils.SafeFilePath("/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