This article from InSource shows how to use the StreamReader() .Net Class() to access data from a .csv file and iterate through each of the rows and records within those rows.
- Author: Michael Walker
- Published: 03/16/2016
- Applies to: Wonderware Application Server (All Versions)
Data in a file can be read from the file system using the System.IO.StreamReader() class. The variable that is created as a System.IO.StreamReader is set to a file on the file system, using theSystem.IO.File()’s OpenText() method. This sends the data in the file into the StreamReader() variable. Once the variable has gained access to the data in the file, it can be iterated through and used appropriately. To iterate through each line of the file, theSystem.IO.StreamReader.Peek() method is used. The Peek() method will allows access to every row in the file, until it reaches the end of the file or the application exits the Peek() method call.The code sample below takes the data from the file "C:\CSVTest\CSVFile.csv" and reads that into an array of string. The array of string is then looped through to build another string used as output into the System Management Console log viewer. The figure below demonstrates the code for this functionality.
The figure below represents the output of the script above in the System Management Console Log Viewer. Note that each record in the .CSV was accessed individually and appended on a new line of the message sent to the System Management Console. This could be used to set individual attributes on an object, or custom properties on an Archestra Graphic from data within a .CSV file.
Attached is the .CSV file and script that can be used to run this example. Note that the script is hard coded to interact with a directory of "C:\CSVTest", so this directory would need to be present where the object or graphic is executing for the example to work.