[C# Grasshopper Rhino6] Where should i put .csv file which is loaded from GHComponent?

Hi,

I’m trying to make component which can handle C# forms.


My form will read .csv file when it is showed up.

It works well in Rhino5(both debugging mode and normal launching)
But in Rhino6, my form won’t be showed up.

I found that this error is caused by file location problem.

I usually save .csv file in “C:\Users\UserName\AppData\Roaming\Grasshopper\Libraries”.
In case of Rhino6, Where shoud I put .csv file ?

Best,
Shimpei

1 Like

I’m not sure, but sometimes turning off COFF settings could solve such problems. you can do this by running GrasshopperDeveloperSettings command in rhino:
COFF

1 Like

Hi Mahdiyar,

Thank you for replying me!

I tried this but my problem didn’t change.

However, I found the solution to solve it.

string path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
StreamReader sectReader = new StreamReader(path+@"\Grasshopper\Libraries\section.csv");

Best
Shimpei

1 Like

If possible, post the relevant CSV reader code.

I’d recommend to resolve the relative path yourself to ensure your plugin works whatever loading mechanism is.

1 Like

Hi gankeyu,

Thank you for replying me!
Pancake is good plugin so I think you are an expert of that field(handling data export and import).

Here is my code.

        Utilities.sectionform _sectionform;

        public void DisplayForm1()
        {
            _sectionform = new Utilities.sectionform();
            string path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            StreamReader sectReader = new StreamReader(path+@"\Grasshopper\Libraries\section.csv"); 
            _sectionform.FormClosed += OnFormClosed;
            GH_WindowsFormUtil.CenterFormOnCursor(_sectionform, true);
            _sectionform.Show(Grasshopper.Instances.DocumentEditor);
        }

        private void OnFormClosed(object sender, FormClosedEventArgs formClosedEventArgs)
        {
            _sectionform = null;
        }

        protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu)
        {
            Menu_AppendItem(menu, "Show Section Editor", ShowUiClicked1, null, true, false);
        }

        private void ShowUiClicked1(object sender, EventArgs e)
        {
            DisplayForm1();
        }

Best,
shimpei

1 Like

Just some information

I suppose you were not using System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) before you found a solution.

Without an absolute path, for example: var sectReader = new StreamReader("section.csv");
Dotnet framework will look up the file in the current working directory, which isn’t guaranteed to be Libraries folder.

If the file is not there, a FileNotFoundException will be raised. Since the constructor isn’t enclosed in a try...catch clause, the entire method will abort and _sectionform.Show never gets executed.

From a programming paradigm perspective, I’d recommend you use try...catch to show a message instead of silent fail, e.g.:

    public void DisplayForm1()
    {
        string path = System.Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        StreamReader sectReader = null;
        try
        {
            sectReader = new StreamReader(path+@"\Grasshopper\Libraries\section.csv"); 
        } catch (FileNotFoundException e) {
            MessageBox.Show("CSV file not found");
            return;
        }
        _sectionform = new Utilities.sectionform();
        _sectionform.FormClosed += OnFormClosed;
        GH_WindowsFormUtil.CenterFormOnCursor(_sectionform, true);
        _sectionform.Show(Grasshopper.Instances.DocumentEditor);
    }

PS: Remember to Dispose the StreamReader or use using(...) clause to prevent potential resource leak. example

1 Like

A post was split to a new topic: C# csv to List script