如何在 vb.net 中读写逗号分隔的文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23029298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 17:20:57  来源:igfitidea点击:

How to read and write comma delimited text files in vb.net

vb.netcsvvb6

提问by user3525310

I am currently converting some old VB6 code to VB.net 2013 and am having difficulty reading and writing comma delimited text files. The old VB6 read/write code was straightforward using the Input statement and storing the comma delimited values in the following variables.

我目前正在将一些旧的 VB6 代码转换为 VB.net 2013,并且在读写逗号分隔的文本文件时遇到困难。旧的 VB6 读/写代码很简单,使用 Input 语句并将逗号分隔的值存储在以下变量中。

Open FileName For Input As #1

  Input #1, A, B, C, D
  Input #1, E, F, G, H
  Input #1, I, J, K, L

Close #1

The user would then have the ability to save the file changes back to the comma delimited text file using the Write statement as follows:

然后,用户可以使用 Write 语句将文件更改保存回逗号分隔的文本文件,如下所示:

Open FileName For Output As #1

   Write #1, A, B, C, D
   Write #1, E, F, G, H
   Write #1, I, J, K, L

Close #1

This should be a fairly simple task in VB.NET but I am having trouble getting it right. Any help is appreciated.

这在 VB.NET 中应该是一项相当简单的任务,但我无法正确完成。任何帮助表示赞赏。

采纳答案by Hossein Salmanian

consider that you have some string value like this Name = "Tom" LastName = "Sanders" Salary = "25000"so you should import the System.IO namespace and then write code for write the values to text file same as below

考虑到你有一些像这样的字符串值 Name = "Tom" LastName = "Sanders" Salary = "25000"所以你应该导入 System.IO 命名空间,然后编写代码将值写入文本文件,如下所示

Dim Name, LastName, Salary As String
Name = "Tom"
LastName = "Sanders"
Salary = "25000"


Dim wr As StreamWriter
wr = New StreamWriter("C:\SampleTextFile.txt")
wr.WriteLine(String.Format("{0},{1},{2}", Name, LastName, Salary))
wr.Flush()
wr = Nothing