Save file directory

unhandled

#1

Hi,

I would like to save file additional items when you right click on component.
I just save a string file.

The issue I have is that the dialog box that is opened to save files, have no address bar. I literally have to click multiple times until I navigate to certain folder. Is there any way I could have a more comfortable dialog box like open dialog box (open dialog box does not allow to save files).

The code I have for this dialog box in grasshopper component is this:
Any help would be much appreciated and save lots of my clicking time.

    protected override void AppendAdditionalComponentMenuItems(ToolStripDropDown menu) {

        Menu_AppendItem(menu, "Select Directory...", (_, __) => {

            var folderDialog = new FolderBrowserDialog();
            var run = folderDialog.ShowDialog();

            if (run == DialogResult.OK) {
                directory = folderDialog.SelectedPath;
                System.IO.File.WriteAllLines(folderDialog.SelectedPath + "/" + filename, GCode);
                Rhino.RhinoApp.WriteLine(directory);
                Rhino.RhinoApp.WriteLine((GCode.Count > 0).ToString());
                ExpireSolution(true);
            }

        });

    }

(David Rutten) #2

You mean SaveFileDialog?


#3

Thanks it works well.

Do I need to add ExpireSolution somewhere between these lines or not?

            Menu_AppendItem(menu, "Select Directory with Address...", (_, __) => {

                SaveFileDialog saveFileDialog1 = new SaveFileDialog();

                saveFileDialog1.FileName = filename;
                saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
                saveFileDialog1.FilterIndex = 2;
                saveFileDialog1.RestoreDirectory = true;

                if (saveFileDialog1.ShowDialog() == DialogResult.OK) {

                    using (System.IO.StreamWriter sw = new System.IO.StreamWriter(saveFileDialog1.FileName)) {
                        foreach(string s in GCode)
                            sw.WriteLine(s);
                        //GCode
                    }

                    //ExpireSolution(true);

                }

            });

(David Rutten) #4

I don’t know, what’s GCode? It seems as though you’re only doing some file writing inside the handler, not changing the runtime state of the component, so based on that you don’t need to expire the solution.

But I don’t know exactly what it is you want to do.

ps. I’d recommend using System.IO.File.WriteXXXXX methods instead of messing about with streams and writers.


#5

Just out of curiosity: Is this a Grasshopper specific recommendation or are there general reasons not to use StreamWriter/StreamReader ?


#6

Thanks

It is just a list of strings, and I am saving it to file.

I have two options that one and
System.IO.File.WriteAllLines(folderDialog.SelectedPath + “/” + filename, GCode);
Both of them produces result I want.


(David Rutten) #7

Grasshopper doesn’t enter into it here, just in general if you’re reading or writing entire files in one go you’re better off using the File methods. Streams and writers are useful mostly when you want to do piecemeal or partial reading or writing.