none
[vb2005] 不懂儲存文字檔 RRS feed

  • 問題

  • Code Snippet

    Dim myStream As IO.Stream
    Dim saveFile As New SaveFileDialog()

    Dim item As String


    saveFileDialog.FileName = "IncomeTax.txt"
    saveFileDialog.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"
    saveFileDialog.FilterIndex = 1

    If saveFileDialog.ShowDialog() = DialogResult.OK Then

    myStream = saveFileDialog.OpenFile()

    If (myStream IsNot Nothing) Then

    For Each item In lstOutput.Items
    file.WriteLine(item)
    Next

    myStream.Close()
    ToolStripStatusLabel1.Text = "Save success"
    End If
    End If


    我只懂寫到這樣...求大大幫忙除錯


    ListBox =>
    saveFileDialog => text file
    2008年10月30日 上午 12:56

解答

  • 請將:

     

    myStream = saveFileDialog.OpenFile()

    If (myStream IsNot Nothing) Then

    For Each item In lstOutput.Items
    file.WriteLine(item)
    Next

    myStream.Close()

     

    改成

     

    Dim sw As New StreamWriter(SaveFileDialog1.FileName, False, Encoding.Default)

    sw.Write("欲寫入檔案的資料")

    sw.Close()

     

    tihs

    2008年10月30日 上午 02:33

所有回覆

  • 請將:

     

    myStream = saveFileDialog.OpenFile()

    If (myStream IsNot Nothing) Then

    For Each item In lstOutput.Items
    file.WriteLine(item)
    Next

    myStream.Close()

     

    改成

     

    Dim sw As New StreamWriter(SaveFileDialog1.FileName, False, Encoding.Default)

    sw.Write("欲寫入檔案的資料")

    sw.Close()

     

    tihs

    2008年10月30日 上午 02:33
  •  tihs 寫信:

    請將:

     

    myStream = saveFileDialog.OpenFile()

    If (myStream IsNot Nothing) Then

    For Each item In lstOutput.Items
    file.WriteLine(item)
    Next

    myStream.Close()

     

    改成

     

    Dim sw As New StreamWriter(SaveFileDialog1.FileName, False, Encoding.Default)

    sw.Write("欲寫入檔案的資料")

    sw.Close()

     

    tihs

    Code Snippet

    Dim item As String

    Dim myStream As IO.Stream

    Dim saveFile As New SaveFileDialog()

    Dim sw As New IO.StreamWriter(saveFile.FileName, False, Encoding.Default)

    saveFile.FileName = "IncomeTax.txt"

    saveFile.Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*"

    saveFile.FilterIndex = 1

    If saveFile.ShowDialog() = DialogResult.OK Then

    myStream = saveFile.OpenFile()

    If (myStream IsNot Nothing) Then

    For Each item In lstOutput.Items

    sw.Write(item)

    Next

    myStream.Close()

    ToolStripStatusLabel1.Text = "Save success"

    End If

    End If

     

     

    Encoding 有問題
    2008年10月30日 上午 05:58
  • Hi,

    要使用Encoding要先Imports System.Text的命名空間。

     

    2008年10月30日 下午 12:42
    版主
  • 感謝2位大大幫忙
    我成功了^^
    2008年10月30日 下午 01:58