How to change 2 types of file extensions on one button click in c#? -
i change more file extensions @ same time, example change .txt files .txt.no , .jpg.no .jpg
my code looks this:
private void button_click(object sender, eventargs e) { directoryinfo d = new directoryinfo(appdomain.currentdomain.basedirectory); fileinfo[] infos = d.getfiles(); foreach (fileinfo f in infos) { file.move(f.fullname, f.fullname.tostring().replace(".txt", ".txt.no")); file.move(f.fullname, f.fullname.tostring().replace(".jpg.no", ".jpg")); } }
i , error when try 2 changes @ once. when using first file.move
line alone program working.
when rename file in first line, second fail because file no longer has name.
you need condition based on file extension.
foreach (fileinfo f in infos) { if(f.fullname.endswith(".txt")) file.move(f.fullname, f.fullname.tostring().replace(".txt", ".txt.no")); else if(f.fullname.endswith(".jpg.no")) file.move(f.fullname, f.fullname.tostring().replace(".jpg.no", ".jpg")); }
Comments
Post a Comment