Free Unity Tutorials

C# Importing Data From Files

Visual Studio, C# Console Application: System.IO, LINQ, Generic.Collections

by Charles Burt on 16/06/2019

The Problem:
Data isn’t always already populated into an array for use. It sometimes sits in a document on your hard disk or on the internet. This tutorial covers files that sit on your hard disk.

The Solution:
Use System.IO to read the file.
Use System.Collections.Generic to store the data.
Use System.Linq to filter the data.

Implementation Steps start from 7 minutes 55 seconds into the video above.

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;

FruitName	Apple	DaystToExpiry	12	SupplierLocation	London	SupplierName	Tesco	AvailableStock	10
FruitName	Pear	DaystToExpiry	11	SupplierLocation	London	SupplierName	Lidl	AvailableStock	5
FruitName	Apple	DaystToExpiry	10	SupplierLocation	Paris	SupplierName	Sainsburies	AvailableStock	1
FruitName	Apple	DaystToExpiry	11	SupplierLocation	NewYork	SupplierName	Costco	AvailableStock	90
FruitName	Apple	DaystToExpiry	5	SupplierLocation	NewYork	SupplierName	Wall Mart	AvailableStock	1
class Fruit
    public string FruitName, DaysToExpiry, SupplierLocation, SupplierName, AvailableStock;
class Program
    static void Main(string[] args)
        string DataPath = "D:/OneDrive/Docs/Source/ Data From Files/Importing Data From Files";
        string FileName = "FruitStockData.txt";
        string dataFile = DataPath + Path.DirectorySeparatorChar + FileName;

        string[] FruitSupplierTxt = File.ReadAllLines(dataFile).Where(arg => !string.IsNullOrWhiteSpace(arg)).ToArray();

        Fruit[] FruitArray = new Fruit[FruitSupplierTxt.Length];

        for (int i = 0; i < FruitArray.Length; i++)
            FruitArray[i] = new Fruit();

        for (int i = 0; i < FruitSupplierTxt.Length; i++)
            string[] sections = FruitSupplierTxt[i].Split('\t');

            FruitArray[i].FruitName = sections[1];
            FruitArray[i].DaysToExpiry = sections[3];
            FruitArray[i].SupplierLocation = sections[5];
            FruitArray[i].SupplierName = sections[7];
            FruitArray[i].AvailableStock = sections[9];

        // I want to manually insert one blank entry into the imported dataset, rather than the txt file
        //Fruit BlankFruit = new Fruit() { FruitName = "BLANK", SupplierName = "BLANK", AvailableStock = "BLANK", SupplierLocation = "BLANK", DaysToExpiry = "BLANK" };
        //FruitArray = FruitArray.Append(BlankFruit).ToArray();

        // I want to manually insert some blank entries into the imported dataset for each location, if less than 5 entries for that location exist
        //int targetEntriesPerLocation = 5;

        //var groupedLocations = FruitArray.GroupBy(fruit => fruit.SupplierLocation);

        //List<Fruit> extendedArray = new List<Fruit>();

        //foreach (var locationGroup in groupedLocations)
        //    int entriesPerLocation = (int)locationGroup.LongCount();

        //    for (int i = entriesPerLocation; i < targetEntriesPerLocation; i++)
        //    {
        //        Fruit BlankFruit = new Fruit() { FruitName = "BLANK", SupplierName = "BLANK", AvailableStock = "BLANK", SupplierLocation = locationGroup.Key, DaysToExpiry = "BLANK" };
        //        extendedArray.Add(BlankFruit);
        //    }

        //FruitArray = FruitArray.Concat(extendedArray).ToArray();

        //FruitArray = FruitArray.OrderBy(fruit => fruit.SupplierLocation).ToArray();

        foreach (var fruitEntry in FruitArray)
            Console.WriteLine("Supplier Name: " + fruitEntry.SupplierName + ", Location: " + fruitEntry.SupplierLocation);
            //Do other things here..



Share on facebook
Share on twitter
Share on linkedin

Charles Burt

Unity Developer

Author Bio

This tutorial was written by me, Charles Burt. Nice to meet you!

I have over a decade of experience in creating software using the Unity Editor and setting up teams around projects.

If you have a project you need help with, or know someone that does, click the HIRE ME button at the bottom of the page! 

Thank you and I hope you found this tutorial helpful.