2010-02-19 19 views

Respuesta

0

de ahorrar

// fetch the selected Text from your list 
    string textToRight = listBox1.SelectedItem.ToString(); 

    // Write to a file  
    StreamWriter sr = File.CreateText(@"testfile.txt");  
    sr.Write(textToRight); 
    sr.Close(); 

mensaje

// display Message 
    MessageBox.Show("Information Saved Successfully"); 
+0

** Olvidó cerrar su StreamWriter **. – SLaks

+0

han arreglado, gracias. –

1

Un SaveFileDialog se utiliza con ShowDialog() para mostrar al usuario, y si tiene éxito, utilizando su OpenFile() para obtener el (Archivo) Stream que usted escribe a. Hay un ejemplo en el msdn page.

A ListBox se puede acceder a través de su propiedad Items, que es simplemente una colección de los elementos que contiene.

0

Tiene algunas cosas en juego allí; asegúrese de dividirlas, p.

  • el contenido de la caja? Recibir
  • Anexar información
  • escribir el archivo de

Tenga en cuenta !! Hay un gran número de excepciones, usted puede obtener mientras se guarda un archivo, ver los documentos y manejar de alguna manera ...

// Get list box contents 
var sb = new StringBuilder(); 
foreach (var item in lstBox.Items) 
{ 
    // i am using the .ToString here, you may do more 
    sb.AppendLine(item); 
} 
string data = sb.ToString(); 

// Append Info 
data = data + ????.... 

// Write File 
void Save(string data) 
{ 
    using(SaveFileDialog saveFileDialog = new SaveFileDialog()) 
    { 
     // optional 
     saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyComputer); 

     //saveFileDialog.Filter = ???; 

     if (saveFileDialog.ShowDialog() == DialogResult.OK) 
     { 
      File.WriteAllText(saveFileDialog.Filename); 
      MessageBox.Show("ok", "all good etc"); 
     } 
     else 
     { 
     // not good...... 
     } 
    } 
} 
+0

arregla tu formateo de bloque de código, por favor ...;) – IAbstract

+0

sí - ¡eso fue feo! (hecho) –

3

esto debería hacerlo.

private void button1_Click(object sender, EventArgs e) 
{ 
    OpenFileDialog f = new OpenFileDialog(); 

    f.ShowDialog();     

    ListBox l = new ListBox(); 
    l.Items.Add("one"); 
    l.Items.Add("two"); 
    l.Items.Add("three"); 
    l.Items.Add("four"); 

    string textout = ""; 

    // assume the li is a string - will fail if not 
    foreach (string li in l.Items) 
    { 
     textout = textout + li + Environment.NewLine; 
    } 

    textout = "extra stuff at the top" + Environment.NewLine + textout + "extra stuff at the bottom"; 
    File.WriteAllText(f.FileName, textout); 

    MessageBox.Show("all saved!"); 
} 
+2

'OpenFileDialog'? o 'SaveFileDialog'? – spajce

3
 var saveFile = new SaveFileDialog(); 
     saveFile.Filter = "Text (*.txt)|*.txt"; 
     if (saveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
     { 
      using (var sw = new StreamWriter(saveFile.FileName, false)) 
       foreach (var item in listBox1.Items) 
        sw.Write(item.ToString() + Environment.NewLine); 
      MessageBox.Show("Success"); 
     } 

Asimismo, tomamos nota de la StreamWriter tiene un tipo de Encoding.

+0

Posible duplicado: http://stackoverflow.com/questions/3336186/saving-listbox-items-to-file?rq=1 –

+0

Totally awesome .. –

Cuestiones relacionadas