2010-02-19 28 views

cevap

0

kaydet

// 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(); 

İleti

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

** StreamWriter ** 'ı kapatmayı unutmuşsunuzdur. – SLaks

+0

sabittir, teşekkürler. –

1

A SaveFileDialog kullanıcıya göstermek için ShowDialog() ile kullanılır ve başarılı olursa, Stream (Dosya) almak için onun OpenFile() kullandığını yazarsın msdn page'da bir örnek var. A ListBox numaralı telefondan, Items numaralı telefondan erişilebilen, yalnızca üzerindeki öğelerden oluşan bir koleksiyon.

0

Orada devam eden birkaç şey var - bunları ayırdığınızdan emin olun, örn.

  • alın liste kutusu içindekiler
  • Ekleme Bilgisi
  • Yaz Dosya

unutmayın !! bunu yapmalısınız

// 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

kod blok biçimlendirmenizi düzeltin, lütfen ...;) – IAbstract

+0

evet - bu çirkindi! (tamamlandı) –

3

bir dosyayı kaydederken, almak docs görmek ve onları bir şekilde işleyebilir istisnalar sayısız ... yoktur.

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'? veya '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"); 
     } 

Ayrıca StreamWriterEncoding bir yazın vardır not alın.

+0

Olası kopya: http://stackoverflow.com/questions/3336186/saving-listbox-items-to-file?rq=1 –

+0

Tamamen harika .. –

İlgili konular