Saturday, 15 February 2014

Insert string into a filepath string before the file extension C# -



Insert string into a filepath string before the file extension C# -

i have string defines path of file:

string duplicatefilepath = d:\user\documents\processed\duplicate_files\file1.jpg;

i going move file location file identical name exists ready. in case want differentiate filename. have crc value of each file available figured may utilize ensure individual file names. can create:

string duplicatefilepathwithcrc = duplicatefilepath + "(" + crcvalue + ")";

but gives:

d:\user\documents\processed\duplicate_files\file1.jpg(crcvalue);

and need:

d:\user\documents\processed\duplicate_files\file1(crcvalue).jpg;

how can set crcvalue string before file extension, bearing in mind there other .'s in file path , file extensions vary?

use static methods in system.io.path class split filename , add together suffix before extension.

string addsuffix(string filename, string suffix) { string fdir = path.getdirectoryname(filename); string fname = path.getfilenamewithoutextension(filename); string fext = path.getextension(filename); homecoming path.combine(fdir, string.concat(fname, suffix, fext)); } string newfilename = addsuffix(filename, string.format("({0})", crcvalue));

c#

No comments:

Post a Comment