Creates a valid filename by replacing all invalid characters with the string provided.
Syntax
Parameters
- filename
- replaceWithChars
Exceptions
Exception | Description |
System.ArgumentException | The exception that is thrown when one of the arguments provided to a method is not valid. |
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