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.

  1. replace oldtext1 filename1
  2. replace oldtext2 filename2
  3. 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

Popular posts from this blog

c# - Better 64-bit byte array hash -

webrtc - Which ICE candidate am I using and why? -

php - Zend Framework / Skeleton-Application / Composer install issue -