How to Import csv using C# from a local drive file path?

Hello,

I’m sorry if this has been answered before, I have been searching everywhere trying to find an answer. I am using the latest version of unity and visual studio. I want to import a csv from a local drive file path and then every 30 minutes or so refresh the file. The reason I need to use a C# script is because the data is constantly being updated. I thought I could use System.IO.File but I’m not able to find any examples of code for it.

Could you please help with an example or point me to another forum.

System.IO.File can read local files. Test that first. It is going to require the correct path and more importantly the correct operating system security settings.

When it fails, check the device log.

Once you have the file open, there are some C# CSV libraries you can use but the format is simple enough you might find it easier to just write the parsing logic yourself, depending on how reliable the formatting is.

Remember that CSV is an ambiguously-defined format and everybody does it a little bit differently. :slight_smile:

Here is an example from microsoft.com
File Class (System.IO) | Microsoft Learn

using System;
using System.IO;

using (StreamReader sr = File.OpenText(path))
        {
            string s;
            while ((s = sr.ReadLine()) != null)
            {
                Console.WriteLine(s);
            }
        }

Close the reader when finished *

He did. See the using() statement. :slight_smile:

Just incase he does the full variable Stream reader Shabam

Thank you everyone for the help.

Thank you for this answer it worked. However, In Unity Console.Writeline(s); isn’t showing the data in my console. Which isn’t a huge deal I changed it to Debug.Log(s); and can see the data is there so I should be able to work with it now.

I have another feature I’m trying to implement and I’m hoping someone can point me in the right direction. Now that I can read in the data. I want to display each of the row data on its own draggable object. (I’m thinking maybe a UI Image might be the best but I’m not sure). The only tutorials or forums I’ve found are turning it into a text asset or a ScriptableObject but the tutorials don’t show how to use them after they are created.

Also this is something that needs done at runtime and that can be updated with a refresh button. So I’m not sure if a text asset or scriptable object would be the right direction I need.

I know how to make something draggable, that was pretty easy to find but I can’t find anywhere someone displaying their csv data as text on an object, image, or anything.

Note: the user will not be able to edit the draggable object, if the information is updated it will be through the csv. Which is the reason I am going to go for a refresh button.

Note: The code below is what I’m experimenting with to read in the data. Currently I just have it Debug.Log the first column without the header.

{
        string csvText = System.IO.File.ReadAllText(csvPath);
        csvText = csvText.Replace("\"", "");
        string[] csvRows = csvText.Split('\n');
       
        string[] headers = csvRows[0].Split(',');

        var rsNbr  = new List<string>();


        /*csvRows = csvRows.Skip(1).ToArray();*/
        for (int i = 1; i < csvRows.Length; i++)
        {
            string[] rowData = csvRows[i].Split(',');
            rsNbr.Add(rowData[0]);

        }
        for (int i = 0; i < rsNbr.Count; i++)
        {
            Debug.Log(rsNbr[i]);
        }

    }

Enclosed is an example of dynamically updating a collection of buttons at runtime.

It uses the assumption that you author a “master” or “exemplar” button in your scene that the code proceeds to clone and customize based on the live data, in this case sprites and their names, but the idea extends to any data.

Also, always best to start fresh posts for fresh topics, as per forum guidelines. :slight_smile:

8494565–1130474–DynamicUIDemo.unitypackage (55.7 KB)