C# : Parsing information out of a path
Posted
by mbcrump
on Geeks with Blogs
See other posts from Geeks with Blogs
or by mbcrump
Published on Tue, 20 Apr 2010 14:44:19 GMT
Indexed on
2010/04/20
21:54 UTC
Read the original article
Hit count: 229
If you have a path for example: \\MyServer\MyFolderA\MyFolderB\MyFile.jpg
and you need to parse out the filename, directory name or the directory parent name. You should use the fileinfo class instead of a regex expression or a string split. See the example code below:
- using System;
- using System.IO;
- class Test
- {
- static void Main(string[] args)
- {
- string file = @"\\MyServer\MyFolderA\MyFolderB\MyFile.jpg";
- FileInfo fi = new FileInfo(file);
- Console.WriteLine(fi.Name); // Prints File.jpg
- Console.WriteLine(fi.Directory.Name); // Prints FolderB
- Console.WriteLine(fi.Directory.Parent.Name); // Prints FolderA
- }
- }
© Geeks with Blogs or respective owner