在VB.NET中需要帮助打开和关闭文件。 [英] Need a assist with opening and closing files in VB.NET.
本文介绍了在VB.NET中需要帮助打开和关闭文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
Greetings,
I'm a old time VBA and Basic programmer. I need a assist with opening and closing files in VB.NET. The CSV and the code is below. Can someone assist me with converting this to VB.NET. I will appreciated it. Thanks
"James","ll","ddd",473.57,12700.00,0.03,,,"jan"
"Frank","rr","lll",392.15,8600.00,0.04,"GTD","o","feb"
"Nikola","C","ff",366.55,6900.00,0.05,,,"march"
"Harrison","df","rr",360.76,6900.00,0.05,,,"aug"
"Mark","tm","gg",358.60,8200.00,0.04,,,"julu"
open "C:\Users\john\Desktop\data5.txt" for input as 1
open "C:\Users\john\Desktop\output.txt" for output as 2
Input #1,name1,code1,code2,num1,num2,num3,num4,scode1,scode2,date1
'Calcuations section
write #2,Name1, Evaluation
close 1
close 2
我的尝试:
我试过
What I have tried:
I have tried
FileOpen(filenum, "data5.txt", OpenMode.Input)
推荐答案
例如:
For example like this :
Imports System.IO
Public Sub LoadData()
Dim myFile As String = "c:\Data\myFile.DAT"
Dim fs As FileStream = Nothing
Dim CSV As StreamReader = Nothing
Dim rowdata As String
Try
' open the File
fs = New FileStream(myFile, FileMode.Open, FileAccess.Read)
CSV = New StreamReader(fs)
' load one row
rowdata = CSV.ReadLine
CSV.Close()
Catch ex As Exception
CSV.Close()
End Try
End Sub
在VB.NET中读写文件与VB6 / VBA有显着差异。它可能更接近 FileSystemObject [ ^ ],这是唯一的方法从VBScript读取和写入文件。
MSDN有一些很好的文档:
如何:从Visual Basic中的文本文件中读取 [ ^ ]
如何:在Visual Basic中使用StreamWriter将文本写入文件 [ ^ ]
在这种情况下,您似乎正在尝试从CSV文件中读取。有一个内置的类来帮助解决这个问题,或者你可以使用第三方库。
如何:在Visual Basic中读取逗号分隔的文本文件 [ ^ ]
CsvHelper [ ^ ]
因此,例如:
Reading and writing files in VB.NET is significantly different to VB6 / VBA. It's probably closer to the FileSystemObject[^], which was the only way to read and write files from VBScript.
MSDN has some good documentation:
How to: Read From Text Files in Visual Basic[^]
How to: Write Text to Files with a StreamWriter in Visual Basic[^]
In this case, it looks like you're trying to read from a CSV file. There is a built-in class to help with this, or you can use a third-party library.
How to: Read From Comma-Delimited Text Files in Visual Basic[^]
CsvHelper[^]
So, for example:
Using reader As New Microsoft.VisualBasic.FileIO.TextFieldParser("C:\Users\john\Desktop\data5.txt")
reader.TextFieldType = FileIO.FieldType.Delimited
reader.SetDelimiters(",")
Using writer As New StreamWriter("C:\Users\john\Desktop\output.txt")
While Not reader.EndOfData
Dim currentRow As String() = reader.ReadFields()
Dim name1 As String = currentRow(0)
Dim code1 As String = currentRow(1)
Dim code2 As String = currentRow(2)
' Converting strings to numbers :-
' this can throw an exception if the string is not a valid number.
Dim num1 As Double = Double.Parse(currentRow(3))
Dim num2 As Double = Double.Parse(currentRow(4))
Dim num3 As Double = Double.Parse(currentRow(5))
Dim num4 As Double = Double.Parse(currentRow(6))
Dim scode1 As String = currentRow(7)
Dim scode2 As String = currentRow(8)
Dim date1 As String = currentRow(9)
' Calcuations section
writer.WriteLine("""{0}"",""{1}""", name1, Evaluation)
End While
End Using
End Using
使用Statement(Visual Basic) [ ^ ]
Double.Parse方法(系统) [ ^ ]
String.Format Method(System) [ ^ ] (解释传递给 WriteLine的看起来很滑稽的字符串
方法)
Using Statement (Visual Basic)[^]
Double.Parse Method (System)[^]
String.Format Method (System)[^] (explains the funny-looking string passed to the WriteLine
method)
这篇关于在VB.NET中需要帮助打开和关闭文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文