Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
3Activity
0 of .
Results for:
No results containing your search query
P. 1
Net Framework File I/O

Net Framework File I/O

Ratings:

4.67

(3)
|Views: 687|Likes:
Published by jfroig9781
Introduction to .NET Framework File I/O class library for Visual Basic programmers
Introduction to .NET Framework File I/O class library for Visual Basic programmers

More info:

Published by: jfroig9781 on Oct 07, 2008
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

05/09/2014

pdf

text

original

 
 
Agilent Developer Network
White Paper 
 
Storing Test Data to FileUsing Microsoft 
®
Visual Basic 
®
.NET 
December 2002
Keith Hill
 Agilent Technologies
Summary:
This white paper introduces the .NET Framework Class Library’s file I/Ofunctionality that is available to Microsoft
®
Visual Basic .NET programmers. Fiveexamples are presented that demonstrate various ways to save and retrieve testdata from a file. In addition, examples are presented to demonstrate readingapplication configuration files, file and directory manipulation and monitoring files.
Contents
Introduction
Microsoft Visual Basic .NET (VB.NET) is a significant new release and a worthysuccessor to Visual Basic 6.0. VB.NET introduces language advances, a new runtimeengine and a significantly more powerful runtime library. The language changesintroduce full support for object-oriented programming and structured exceptionhandling. The new runtime engine, called theCommon Language Runtime(CLR)provides better memory management and support for multiple threads. The.NETFramework Class Library(FCL) provides significantly more functionality than the VB6 runtime library. In particular, the FCL provides more options for storing test datato file and for manipulating files and directories in general.Before exploring the new file I/O functionality in the FCL, it is worth pointing out thatthe VB 6 approach to file I/O using VB 6 statements like
Open
,
Print
,
Input
,
Close
 is still available in VB.NET albeit with some syntax changes. For example, in VB 6you would open a file, read from it and close it with the code shown in
Figure 1
.
 
Agilent Developer Network White Paper 2 of 17Storing Test Data to File Using Microsoft
®
Visual Basic
®
.NETNovember 2002
 
Figure 1
Reading a File using VB 6
Dimreading AsDouble  Open "C:\Temp\TestData.txt" For Input As #1Input #1, readingClose #1
In VB.NET, this code changes slightly as shown in
Figure 2
.
Figure 2
Reading a File using VB 6 Compatibility Functions in VB.NET
Dimreading AsDouble  FileOpen(1, "C:\Temp\TestData.txt", OpenMode.Input)Input(1, reading)FileClose(1)
The VB 6 file I/O statements have been replaced by a set of compatibility functionsincluding
FileOpen
,
Print
,
Input
and
FileClose
among others. For moreinformation seeFile Access Types, Functions, and Statements. Despite the syntaxchanges, performing file I/O with these functions is basically the same as it was inVB 6. You can continue to use that approach in VB.NET if you desire. Keep in mindthat the VB 6 compatibility methods mentioned above are merely wrappers on top of the FCL’s native file I/O functionality. Besides just bypassing an extra layer of code,learning how to directly use the FCL’s file I/O objects and methods gives you moreflexibility, an easier mechanism for saving test data and allows you to read file I/Ocode in other .NET languages like C#. The rest of this article explores those nativeFCL file I/O objects and methods.The 1.0 version of the FCL contains more than 3000 public types that contribute over16000 public properties, 33000 public methods and 4000 public events (I told you itcontained significantly more functionality than the VB 6 runtime library). Fortunatelythis functionality is organized into many different namespaces like
System
,
System.Collection
,
System.Net
,
System.Text
,
System.Threading
,
System.Windows.Forms
just to name a few. The file I/O functionality is located inthe
namespace.There are a number of file I/O examples used throughout the rest of this article. Allof the examples dealing with test data use the data format shown in
Figure 3
toillustrate the different ways to save and retrieve test data.
 
Figure 3
Test Data Elements
Name Data Type Description
dutId String Device under test identifiertimestamp DateTime Time data was acquiredstationId Integer Test station identifierdata Double() 3 test data valuesThe example code associated with this paper can be downloaded fromhttp://www.agilent.com/find/adnwhitepaperexamples.
Visit the Agilent Developer Network at http://www.agilent.com/find/ADN
 
 
Agilent Developer Network White Paper 3 of 17Storing Test Data to File Using Microsoft
®
Visual Basic
®
.NETNovember 2002
 
Saving Test Data Using Comma Separated Value Text Format
One very popular text file format is “comma separated value” or CSV. CSV files canbe easily imported by applications like Microsoft Excel.
Figure 4
shows how to savedata in a text file using CSV format.
Figure 4
Writing Data to a CSV Text File
' Example 1a in the sample project' Create test dataDimdutIdAs String= "A00100" DimtimestampAsDateTime = DateTime.Now DimstationIdAs Integer= 1 Dimdata()As Double=New Double() {1.1, 2.2, 3.3} ' Open file for writing textDimwriterAsStreamWriter = File.CreateText(filename) ' Write data in csv (comma separated value) format:' DutIdentifier,Timestamp,StationID,DataUpperBound,Data()writer.Write("{0},{1},{2},{3}", dutId, timestamp, _stationId, UBound(data))DimvalAs Double For EachvalIndata writer.Write("," & val)Next' Close StreamWriter when donewriter.Close()
The following line of code from
Figure 4
demonstrates the use of the
class to open up a file for writing in text mode.
DimwriterAsStreamWriter = File.CreateText(filename)
One interesting tidbit about the
method is that it uses somethingcalled UTF-8 encoding for the text written to file. The advantage to UTF-8 encodingis that ASCII characters require only one byte per character in the file. At the sametime, Unicode characters can be saved although these characters may occupy two ormore bytes per character in the file. There are ways to force exclusive ASCII orUnicode encoded text files. You will see an example of how to do this later.
is another class in the
System.IO
namespace. It defines a numberof overloaded methods for writing various primitive data types in text format. Noticethat the data array is written out preceded by the upper bound. Writing the upperbound is not really necessary especially if the array is a fixed size. However,specifying the upper bound does make it easier to read the data from file and itallows you to change the size of your arrays without breaking code that reads yourfile format. When the code in
Figure 4
executes it produces the file shown in
Figure 5
.
Visit the Agilent Developer Network at http://www.agilent.com/find/ADN
 

Activity (3)

You've already reviewed this. Edit your review.
1 hundred reads
1 thousand reads
Ahmed_20111 liked this

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->