Quick Tip: C# File Info
This is nothing new, but I felt the need to share since so many miss it.
Many times I see stuff like this being done:
string
FolderPath = FilePath.Substring(0, FilePath.LastIndexOf(@"\")); Obviously they are trying to get the full path to a file. Next time your thinking about doing something like that consider:
System.IO.FileInfo fi =
new System.IO.FileInfo(FilePath); string
FolderPath = fi.DirectoryName; Much nicer.
While I'm on the topic take a look at all the other goodies in the System.IO.FileInfo class:
fi.Extension
fi.FullName
fi.Name
fi.Length
fi.Directory.FullName
fi.Directory.Name
fi.Directory.Root.FullName
fi.DirectoryName
Not to mention these methods:
fi.Open();
fi.OpenRead();
fi.OpenText();
fi.OpenWrite();
More: http://msdn.microsoft.com/library/en-us/cpref/html/frlrfSystemIOFileInfoClassTopic.asp