0

i have the listbox1, which content items. This items are names of txt files. These txt files are located on desktop. Question: using which code, c# can refresh my txt file names based on listbox items names. It means, (e.g.) if the first item of listbox is "1"(name before renaming on desktop) then i add a letter "b" in textBox1 then press a button and txt file is automatically refreshed on desktop, but already has name "b" instead of "a" ?

Nick
  • 1
  • 2
  • What have you tried? What is the issue you are facing? Post some relevant code. – Code.me Aug 20 '15 at 22:06
  • for (int i = 0; i < listBox1.Items.Count; i++) { String Text = Convert.ToString(listBox1.Items[i]); Text = Text.Replace(textBox1.Text, textBox2.Text); listBox1.Items[i] = Text; } – Nick Aug 20 '15 at 22:08

1 Answers1

0

So you want to rename text files on your desktop? loop through your list with text names, and in each iteration call this method

System.IO.File.Move("oldfilename", "newfilename");

replace "oldfilename" with the complete file path including name of your text file, and replace "newfilename" with the complete path and new name for the file.

fraser jordan
  • 114
  • 1
  • 9
  • i have all text file names different, i did not write 100 strings with new names.It's crazy. Newilename should be taken by each string in listbox1 – Nick Aug 20 '15 at 22:21
  • i have ~100 txt, i would not write 100 new names – Nick Aug 20 '15 at 22:24
  • OK, first your going to have to get a list of all text file names in your desktop directory, look here: http://stackoverflow.com/questions/14877237/getting-all-file-names-from-a-folder-using-c-sharp Then your going to have to use the method I mentioned in my answer inside a nested for loop to iterate through both lists with new and old names, and change them then. – fraser jordan Aug 20 '15 at 22:27
  • In the method I used above you would use it like this '[System.IO.File.Move("Desktop\" + "oldfilename", "Destop\" + "newfilename");' This would rename the text file 'oldfilename.txt' on my desktop to 'newfilename.txt' – fraser jordan Aug 20 '15 at 22:33
  • i understand all, but i need not write txt files names by fingers, the program must take these new names from listbox and and when i minimize my studio i should see these txt files, but already with names which is taken from listbox – Nick Aug 20 '15 at 22:40
  • yes I know, that's what I'm trying to say, loop through your list box with the names, then for each name assign it to a file using the method i posted – fraser jordan Aug 21 '15 at 02:43