asp.net - Does C# have a method for inserting a string before the extension part of a file name? -
i have bit of code like
if (system.io.file.exists(newfilepath)) { string suffix = (datetime.now.ticks / timespan.tickspermillisecond).tostring(); newfilepath += suffix;
but realized it's incorrect because meant put suffix before extension part of file name (if there 1 @ all).
example:
c://somefolder/someotherfolder/somepic.jpg ---> c://somefolder/someotherfolder/somepic0123913123194.jpg
is there 1-line way this, instead of me spending time handrolling procedure lastindexof
, insert
, etc.
a simple 1 liner be:
newfilepath = string.format("{0}{1}{2}", path.getfilenamewithoutextension(newfilepath), suffix, path.getextension(newfilepath));
edit: or preserve directory too:
newfilepath = string.format("{0}{1}{2}{3}", path.getdirectoryname(newfilepath), path.getfilenamewithoutextension(newfilepath), suffix, path.getextension(newfilepath));
Comments
Post a Comment