开发者

What's the best way to change filename extensions in .NET?

开发者 https://www.devze.com 2023-02-19 02:28 出处:网络
I\'ve got a thumbnailer which scans a given directory and generates thumbnails for the image files it finds.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号