incubuzz1978
incubuzz1978

Reputation: 53

For each listbox item in variable

somehow i seem to be blind this morning ;) i have the following code[1] which does read the object collection out of an ListBox. with the string b i can "retrieve" the strings

in b there are filenames and directory paths stored that i want to backup with xcopy code [2].

Code:

private void btnBenutz_Click(object sender, EventArgs e)
{
    lblAusgabe2.Text = "";
    ListBox.ObjectCollection a = listBox1.Items;
    foreach (string x in a)
    {
        b = x;
        lblAusgabe2.Text += "\n" + b;
    }
}

More code:

 Process.Start("XCOPY.EXE", "/E /I /Y" + b + pfadauswahl + "\\Backup\\" + dt.ToString("yyyy-MM-dd") + "\\UserData\\");

how can i use b as an array which i probably have to ? otherwise only the first item will always been read out? Also the process start i have to use outside of the btnBenutz... so some variable has to be initialized in the public partial class Form2 : Form

Upvotes: 0

Views: 5036

Answers (3)

CodeGuru
CodeGuru

Reputation: 2803

As you commented you want to call it from other places also like another button click then Do something like this :

1) Declare list of string at class level

 List<string> fileNameList ;

2) Create a function with some meaningfull name let's say StartXcopy like below

public void StartXcopy()
{
       ListBox.ObjectCollection a = listBox1.Items;
       fileNameList = new List<string>();
       foreach (string x in a)
       {
            fileNameList.Add(x);
            lblAusgabe2.Text += "\n" + x;
       }

       foreach (string filename in fileNameList)
       {
           System.Diagnostics.Process.Start("XCOPY.EXE", "/E /I /Y" + filename  + pfadauswahl + "\\Backup\\" + dt.ToString("yyyy-MM-dd") + "\\UserData\\");
       }
}

3) Then call this function from where you want, like below in button click

 private void btnBenutz_Click(object sender, EventArgs e)
 {
      lblAusgabe2.Text = "";
      StartXcopy(); 
 }

Note : Here i am assuming you are always iterating through listBox1 items.

Upvotes: 0

Moha Dehghan
Moha Dehghan

Reputation: 18462

Define b as List<string>. You also use a better name like fileNameList:

private List<string> fileNameList; // a class field, not a local variable

Then add the file names to the list:

private void btnBenutz_Click(object sender, EventArgs e)
{
    lblAusgabe2.Text = "";
    ListBox.ObjectCollection a = listBox1.Items;
    foreach (string x in a)
    {
        fileNames.Add(x);
        lblAusgabe2.Text += Environment.NewLine + x; // Why are you doing this?
    }
}

Then in another place, run the xopy command for each file:

foreach(string fileName in fileNameList)
{
    Process.Start("XCOPY.EXE", "/E /I /Y " + fileName + pfadauswahl + "\\Backup\\" + dt.ToString("yyyy-MM-dd") + "\\UserData\\");
}

if that's what you are trying to achieve!

Upvotes: 1

Dennis
Dennis

Reputation: 37770

private void btnBenutz_Click(object sender, EventArgs e)
{
    var sb = new StringBuilder();
    foreach (string x in listBox1.Items)
    {
        sb.Append("\n" + x);
    }
    // then use sb.ToString() somewhere...
}

Upvotes: 0

Related Questions