c# - How to get filenames in a folder 1 by 1 and use them in .txt file -
is possible? im trying here file names directory use file name in text file removing existing text replacing file name. problem having filename1 replaces oldtext in file need 1 filename per oldtext in .txt file
oldtext? = text in .txt want replace file name
ex.
- replace oldtext1 filename1
- replace oldtext2 filename2
replace oldtext3 filename3
and on
alphabetical order ideal.
thankx in advance.
.
directoryinfo dinfo1 = new directoryinfo(path); fileinfo[] files1 = dinfo1.getfiles("*.*"); string text = file.readalltext("path/text.txt"); foreach (fileinfo file in files1) { text = text.replace("oldtext1", "path" + file.name); text = text.replace("oldtext2", "path" + file.name); text = text.replace("oldtext3", "path" + file.name); } file.writealltext("path/text.txt", text)
;
your current code replaces texts inside loop, same file name, after loop done, oldtexts should contain last file name.
you should this:
directoryinfo dinfo1 = new directoryinfo(path); fileinfo[] files1 = dinfo1.getfiles("*.*"); string[] stringstoreplace = {"oldtext1", "oldtext2", "oldtext2"}; string text = file.readalltext("path/text.txt"); for(int i=0; < stringstoreplace.length; i++) { if(i >= files1.length) { break; } text = text.replace(stringstoreplace[i], "path" + files1[i].name); } file.writealltext("path/text.txt", text);
Comments
Post a Comment