保存txt文件中输入框的二维数组值的最佳方法

时间:2013-05-15 19:57:45

标签: .net vb.net

我有几个输入框,我想将所有这些值存储在二维数组中,然后将所有值写入txt文件,这是因为我想知道值的i,j。所以,如果我进行一些更改,我只会看看这个数组,那么如何在txt文件中插入一个数组呢?

我在做

Dim mArray(100,100) As String

mArray(0,0) = Textbox1.Text
mArray(0,1) = Textbox2.Text
...



Dim objWriter As New System.IO.StreamWriter("c:\file.txt", False)
objWriter.WriteLine(mArray)
objWriter.Close()

或者做得更好:

Dim objWriter As New System.IO.StreamWriter("c:\file.txt", False)
objWriter.WriteLine(Textbox1.Text)
objWriter.WriteLine(Textbox2.Text)
objWriter.WriteLine(Textbox3.Text)
objWriter.WriteLine(Textbox4.Text)
...
objWriter.WriteLine(Textboxn.Text)

objWriter.Close()

1 个答案:

答案 0 :(得分:2)

我不知道二维数组,但对于单维数组,这里是代码:

Dim mArray(100) as string
mArray(0) = TextBox1.Text
.
.
.
.

For I as integer = 0 to mArray.count - 1
    Dim objWriter As New System.IO.StreamWriter("c:\file.txt", False)
    objWriter.WriteLine(mArray(I))
    objWriter.Close()
Next I

我认为对于二维数组,你需要使用嵌套的

For I as integer = 0 to ...........
    For J as integer = 0 to ..........
        Dim objWriter As New System.IO.StreamWriter("c:\file.txt", False)
        objWriter.WriteLine(mArray(I,J))
        objWriter.Close()
    Next J
next I