What's the best way to change filename extensions in .NET?
I've got a thumbnailer which scans a given directory and generates thumbnails for the image files it finds.
I've got a nice generic way of loading the images I find which match allowed file extensions (bmp
, jpg
, png
, etc.) but... I want to write all thumbnail files as png
.
What I don't know is how to conveniently extract the filename without extension and stick a .png
on the end before outputting my image开发者_JAVA技巧...
For Each File In SourceFileList
Dim FileInfo = New FileInfo(File)
Dim ThumbFilename = String.Format("{0}\Thumb_{1}", TargetDir, FileInfo.Name) '<-- How do I set the correct name here?
Dim Thumb = Thumbnailer.Thumbnail(FileInfo.FullName, 100)
Thumb.Save(ThumbFilename, Imaging.ImageFormat.Png)
Next
I'm aware I can use Split
and various other string manipulations but all seem a little clunky. Is there a best-practice way of doing this? Something in System.IO
?
(I would never have thought 10 years ago that split
would e too clunky - How times have changed!)
You're looking for Path.ChangeExtension
.
You should also take a look at the rest of the Path
class; it's a very useful but little-known class.
精彩评论