3 Replies - 244 Views - Last Post: 03 August 2012 - 02:10 AM Rate Topic: -----

#1 Youngie1337  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 09

Listbox OpenFile

Posted 02 August 2012 - 03:44 PM

Hi,

Easy one?

I have a listbox1, in this list is say 10 .txt files. I want to open and modify some text strings inside the files (all items in listbox1).

I can pull the directory files in fine, and I can write to the files one by one fine... how do I do multiple?

Using:
          folderBrowserDialog1.ShowDialog();
            textBox3.Text = folderBrowserDialog1.SelectedPath;


Edit text file and write again.
string text = File.ReadAllText(@"c:\test.txt");
            text = text.Replace(textBox1.Text, textBox2.Text);
            File.WriteAllText(@"c:\test.txt", text);


So I basically need this doing for each item inside listbox1.

Any answers?

Thank you.

Is This A Good Question/Topic? 0
  • +

Replies To: Listbox OpenFile

#2 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1914
  • View blog
  • Posts: 5,715
  • Joined: 05-May 12

Re: Listbox OpenFile

Posted 02 August 2012 - 04:30 PM

Yes, you iterate over the contents of the listbox, and take the filename from the listbox, and use that filename instead of the hard coded string you are using.

You must have missed a step, though. In the post above, unless your variable name doesn't match the class type, it looks like you are using the FolderBrowseDialog. That only returns a path to a folder. How are you populating the listbox?
Was This Post Helpful? 0
  • +
  • -

#3 Youngie1337  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 20-January 09

Re: Listbox OpenFile

Posted 03 August 2012 - 01:12 AM

View PostSkydiver, on 02 August 2012 - 04:30 PM, said:

Yes, you iterate over the contents of the listbox, and take the filename from the listbox, and use that filename instead of the hard coded string you are using.

You must have missed a step, though. In the post above, unless your variable name doesn't match the class type, it looks like you are using the FolderBrowseDialog. That only returns a path to a folder. How are you populating the listbox?


Good question, I'm using this method:
            listBox1.Visible = false;
            try
            {
                string[] filePaths = Directory.GetFiles(textBox3.Text);
                listBox1.Items.AddRange(filePaths);
                listBox1.Visible = true;
          
            }
            catch(Exception x)
                {
            
                MessageBox.Show(x.Message, "DOH");
            
                }


Which inserts the path of all files inside the listbox, each appears as a separate item so grabbing the file's location is no problem, but opening each file and writing to that specified location for EACH item in the list is where I'm stuck.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1914
  • View blog
  • Posts: 5,715
  • Joined: 05-May 12

Re: Listbox OpenFile

Posted 03 August 2012 - 02:10 AM

Why can't you just have something like this pseudo-code:
foreach(string path in listBox1.Items) {
    // Do something with file
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1