Creates a valid filename by removing all invalid characters.
Syntax
Visual Basic (Declaration) | |
---|
Public Overloads Shared Function MakeValidFileName( _
ByVal filename As String _
) As String |
Parameters
- filename
Example
Library/Library.Test/TestFileUtils.cs
C# | Copy Code |
---|
Assert.AreEqual("a...", FileUtils.MakeValidFileName("a..."));
Assert.AreEqual("a. . .", FileUtils.MakeValidFileName("a. . . "));
Assert.AreEqual("leading whitespace is bad", FileUtils.MakeValidFileName(" leading whitespace is bad"));
Assert.AreEqual("trailing whitespace is bad", FileUtils.MakeValidFileName("trailing whitespace is bad\t"));
Assert.AreEqual("a_a.txt", FileUtils.MakeValidFileName("a/a.txt", "_"));
Assert.AreEqual("a_a.txt", FileUtils.MakeValidFileName("a|a.txt", "_"));
Assert.AreEqual("a_a._txt", FileUtils.MakeValidFileName("a<|/\\*?>a.*txt", "_"));
Assert.AreEqual(".a", FileUtils.MakeValidFileName(".a"));
Assert.AreEqual(". -", FileUtils.MakeValidFileName(". -"));
Assert.AreEqual("..a", FileUtils.MakeValidFileName("..a"));
Assert.AreEqual("a..", FileUtils.MakeValidFileName("a.."));
Assert.AreEqual("No extension is fine", FileUtils.MakeValidFileName("No extension is fine"));
Assert.AreEqual(". a leading dot is ok", FileUtils.MakeValidFileName(". a leading dot is ok"));
Assert.AreEqual("really really long file extension is actually ok.", FileUtils.MakeValidFileName("really really long file extension is actually ok.")); |
VB.NET | Copy Code |
---|
Assert.AreEqual("a...", FileUtils.MakeValidFileName("a..."))
Assert.AreEqual("a. . .", FileUtils.MakeValidFileName("a. . . "))
Assert.AreEqual("leading whitespace is bad", FileUtils.MakeValidFileName(" leading whitespace is bad"))
Assert.AreEqual("trailing whitespace is bad", FileUtils.MakeValidFileName("trailing whitespace is bad" & vbTab))
Assert.AreEqual("a_a.txt", FileUtils.MakeValidFileName("a/a.txt", "_"))
Assert.AreEqual("a_a.txt", FileUtils.MakeValidFileName("a|a.txt", "_"))
Assert.AreEqual("a_a._txt", FileUtils.MakeValidFileName("a<|/\*?>a.*txt", "_"))
Assert.AreEqual(".a", FileUtils.MakeValidFileName(".a"))
Assert.AreEqual(". -", FileUtils.MakeValidFileName(". -"))
Assert.AreEqual("..a", FileUtils.MakeValidFileName("..a"))
Assert.AreEqual("a..", FileUtils.MakeValidFileName("a.."))
Assert.AreEqual("No extension is fine", FileUtils.MakeValidFileName("No extension is fine"))
Assert.AreEqual(". a leading dot is ok", FileUtils.MakeValidFileName(". a leading dot is ok"))
Assert.AreEqual("really really long file extension is actually ok.", FileUtils.MakeValidFileName("really really long file extension is actually ok.")) |
Requirements
Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7
See Also