Basic
Basic

Reputation: 26766

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...

    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!)

Upvotes: 0

Views: 151

Answers (1)

SLaks
SLaks

Reputation: 888185

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.

Upvotes: 5

Related Questions