2010-02-19 34 views
5

Làm cách nào để lưu nội dung của các mục listbox của tôi vào tệp văn bản bằng cách sử dụng SaveFileDialog?Lưu các mục của hộp danh sách vào một tệp văn bản

Tôi cũng muốn thêm thông tin bổ sung vào tệp văn bản và cũng thêm MessageBox câu nói đã lưu khi thành công.

+0

@roller - điều này đã được giải quyết chưa? –

Trả lời

0

Để Tiết kiệm

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

nhắn

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

** Bạn quên đóng StreamWriter **. – SLaks

+0

đã sửa, cảm ơn. –

1

Một SaveFileDialog được sử dụng với ShowDialog() để hiển thị nó cho người dùng, và nếu nó thành công, sử dụng OpenFile() của nó để có được (File) Stream rằng bạn viết thư cho. Có một ví dụ trên msdn page.

A ListBox có thể truy cập thông qua thuộc tính Items, chỉ đơn giản là một tập hợp các mục trên đó.

0

Bạn có một vài điều đang diễn ra ở đó - hãy đảm bảo bạn chia nhỏ chúng, ví dụ:

  • Nhận hộp danh sách nội dung
  • Nối Thông tin
  • Write file

Xin lưu ý !! Có vô số các trường hợp ngoại lệ bạn có thể nhận được trong khi lưu một file, xem các tài liệu và xử lý chúng bằng cách nào đó ...

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

sửa định dạng mã của bạn, vui lòng ...;) – IAbstract

+0

vâng - đó là xấu xí! (đã hoàn thành) –

3

này nên làm điều đó.

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'? hoặc '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"); 
     } 

Ngoài ra hãy lưu ý StreamWriter có Loại Encoding.

+0

Có thể trùng lặp: http://stackoverflow.com/questions/3336186/saving-listbox-items-to-file?rq=1 –

+0

Hoàn toàn tuyệt vời .. –

Các vấn đề liên quan