C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
First: We call Path.GetFileName, a static method, on a path string. It returns just the end part of the path.
C# program that uses GetFileName
using System;
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\programs\file.txt";
// Get file name.
string filename = Path.GetFileName(path);
Console.WriteLine("PATH: {0}", path);
Console.WriteLine("FILENAME: {0}", filename);
}
}
Output
PATH: C:\programs\file.txt
FILENAME: file.txt
Info: We see that the ".doc" part of the file name is removed—so we are left with the string "example" only.
C# program that uses GetFileNameWithoutExtension
using System;
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\programs\example.doc";
// Get file name without extension.
string filename = Path.GetFileNameWithoutExtension(path);
Console.WriteLine("PATH: {0}", path);
Console.WriteLine("NO EXTENSION: {0}", filename);
}
}
Output
PATH: C:\programs\example.doc
NO EXTENSION: example
Note: GetExtension handles extensions of 4 letters. It also handles the case where a file name has more than one period in it.
Here: This program briefly tests GetExtension. The period is part of the extension string returned.
C# program that uses GetExtension
using System;
using System.IO;
class Program
{
static void Main()
{
// ... Path values.
string value1 = @"C:\Codex\word.txt";
string value2 = @"C:\file.excel.dots.xlsx";
// ... Get extensions.
string ext1 = Path.GetExtension(value1);
string ext2 = Path.GetExtension(value2);
Console.WriteLine(ext1);
Console.WriteLine(ext2);
}
}
Output
.txt
.xlsx
C# program that gets path root
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "C:\\images\\universe.jpg";
// Get path root.
string root = Path.GetPathRoot(path);
Console.WriteLine("PATH: {0}", path);
Console.WriteLine("ROOT: {0}", root);
}
}
Output
PATH: C:\images\universe.jpg
ROOT: C:\
C# program that uses GetDirectoryName
using System;
using System.IO;
class Program
{
static void Main()
{
string path = "C:\\images\\universe.jpg";
// Get directory name.
string result = Path.GetDirectoryName(path);
Console.WriteLine("PATH: {0}", path);
Console.WriteLine("DIRECTORY: {0}", result);
}
}
Output
PATH: C:\images\universe.jpg
DIRECTORY: C:\images
C# program that uses verbatim string
using System;
class Program
{
static void Main()
{
// Verbatim string syntax.
string value = @"C:\directory\word.txt";
string value2 = "C:\\directory\\word.txt";
Console.WriteLine("VALUE: {0}", value);
Console.WriteLine("VALUE2: {0}", value2);
}
}
Output
VALUE: C:\directory\word.txt
VALUE2: C:\directory\word.txt
Info: Path.Combine handles certain cases where we have directory separators in different positions.
Escape: When we have to add the character "\" to a non-verbatim C# string literal, we must use \\ (two backslashes).
Arguments: The arguments to Path.Combine are parts of a path. These can be the volume name, a directory name, or a file name.
C# program that uses Path.Combine
using System;
using System.IO;
class Program
{
static void Main()
{
// Combine 2 path parts.
string path1 = Path.Combine("Content", "file.txt");
Console.WriteLine("COMBINE: {0}", path1);
// Combine handles trailing separator.
string path2 = Path.Combine("Content\\", "file.txt");
Console.WriteLine("COMBINE: {0}", path2);
// Handle 3 parts.
string path3 = Path.Combine("C:\\", "directory", "filename.txt");
Console.WriteLine("COMBINE: {0}", path3);
}
}
Output
COMBINE: Content\file.txt
COMBINE: Content\file.txt
COMBINE: C:\directory\filename.txt
Info: We can call GetRandomFileName many times in a loop and get different results each time.
C# program that uses GetRandomFileName
using System;
using System.IO;
class Program
{
static void Main()
{
for (int i = 0; i < 5; i++)
{
// Get random file.
string result = Path.GetRandomFileName();
Console.WriteLine("RANDOM: {0}", result);
}
}
}
Output
RANDOM: idpqamj0.uba
RANDOM: otyku52c.qlx
RANDOM: e5mxa3p2.4cg
RANDOM: auxhuuav.c2j
RANDOM: epcjprlz.rwr
Note: GetTempPath() has a separator character on the end. Path.GetDirectoryName meanwhile does not.
Note 2: With Path.Combine, we can reliably concatenate a file name with the temporary path received.
Cache: The result of GetTempPath is usually constant throughout program execution. It is safe to cache it. This eliminates allocations.
C# program that uses GetTempPath
using System;
using System.IO;
class Program
{
static void Main()
{
// Get temp path.
string temp = Path.GetTempPath();
Console.WriteLine("TEMP PATH: {0}", temp);
// Create a temp file path.
string combined = Path.Combine(temp, "test.txt");
Console.WriteLine("TEMP FILE: {0}", combined);
}
}
Output
TEMP PATH: C:\Users\Sam\AppData\Local\Temp\
TEMP FILE: C:\Users\Sam\AppData\Local\Temp\test.txt
Next: We examine these 2 properties in the Visual Studio debugger, and see they are the backslash and the forward slash.
Path.DirectorySeparatorChar result
"\\"
Path.AltDirectorySeparatorChar result
"/"
So: We can use the Path.GetInvalidFileNameChars and Path.GetInvalidPathChars methods.
Dictionary: We can use the character arrays returned by Path.GetInvalidFileNameChars and Path.GetInvalidPathChars with a Dictionary.
ToDictionaryC# program that gets invalid characters
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
class Program
{
static void Main()
{
// First, we build a Dictionary of invalid characters.
var dict = GetInvalidFileNameChars();
// Next, we test the dictionary to see if the asterisk (star) is valid.
if (dict.ContainsKey('*'))
{
// This will run.
// ... The star is in the Dictionary.
Console.WriteLine("* is an invalid char");
}
}
/// <summary>
/// Get a Dictionary of the invalid file name characters.
/// </summary>
static Dictionary<char, bool> GetInvalidFileNameChars()
{
// This method uses lambda expressions with ToDictionary.
return Path.GetInvalidFileNameChars().ToDictionary(c => c, c => true);
}
}
Output
* is an invalid char
And: The Request.PhysicalPath value is a Windows-style path. It works well with the Path class.
Code that tests extensions: C#
//
// This could be in your Global.asax file or in an ASPX page.
// It gets the physical path.
//
string physical = Request.PhysicalPath;
//
// Here we see if we are handling an ASPX file.
//
if (Path.GetExtension(physical) == ".aspx")
{
//
// Get the file name without an extension.
//
string key = Path.GetFileNameWithoutExtension(physical);
}
Version 1: This version of the code calls a custom implementation of the Path GetFileNameWithoutExtension method.
Version 2: This code calls the Path method that is built into the .NET Framework. This supports more features.
Result: The custom method is much faster but has slight logic differences. The speed helps when this method is often called.
C# program that benchmarks GetFileNameWithoutExtension
using System;
using System.Diagnostics;
using System.IO;
class Program
{
public static string GetFileNameWithoutExtensionFast(string value)
{
// Find last available character.
// ... This is either last index or last index before last period.
int lastIndex = value.Length - 1;
for (int i = lastIndex; i >= 1; i--)
{
if (value[i] == '.')
{
lastIndex = i - 1;
break;
}
}
// Find first available character.
// ... Is either first character or first character after closest /
// ... or \ character after last index.
int firstIndex = 0;
for (int i = lastIndex - 1; i >= 0; i--)
{
switch (value[i])
{
case '/':
case '\\':
{
firstIndex = i + 1;
goto End;
}
}
}
End:
// Return substring.
return value.Substring(firstIndex, (lastIndex - firstIndex + 1));
}
const int _max = 1000000;
static void Main()
{
const string fileName = @"C:\test\ok.txt";
Console.WriteLine("TEST: {0}={1}",
GetFileNameWithoutExtensionFast(fileName),
Path.GetFileNameWithoutExtension(fileName));
// Version 1: use optimized method.
var s1 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (GetFileNameWithoutExtensionFast(fileName) == null)
{
return;
}
}
s1.Stop();
// Version 2: use Path method.
var s2 = Stopwatch.StartNew();
for (int i = 0; i < _max; i++)
{
if (Path.GetFileNameWithoutExtension(fileName) == null)
{
return;
}
}
s2.Stop();
Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) /
_max).ToString("0.00 ns"));
}
}
Output
TEST: ok=ok
20.68 ns Optimized method
168.30 ns Path method