Пример #1
0
            public static bool SaveDataToFile(TreeView treeView, List <TableGraph> listTableGraph)
            {
                bool res = true;

                try
                {
                    SaveLoadDataSerialize dataSerialize = new SaveLoadDataSerialize(listTableGraph);
                    ToTreeViewSerialize(ref dataSerialize.TreeViewSerialize, treeView.Nodes);

                    string dataString = JsonConvert.SerializeObject(dataSerialize);
                    dataString = JsonPrettify(dataString);

                    using (SaveFileDialog saveFileDialog = new SaveFileDialog())
                    {
                        saveFileDialog.Filter           = _fileFilter;
                        saveFileDialog.InitialDirectory = Environment.CurrentDirectory;
                        if (saveFileDialog.ShowDialog() == DialogResult.OK)
                        {
                            using (StreamWriter streamWriter = new StreamWriter(saveFileDialog.OpenFile()))
                            {
                                streamWriter.Write(dataString);
                                streamWriter.Close();
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                    res = false;
                    MessageBox.Show(e.Message, "Ошибка!");
                }

                return(res);
            }
Пример #2
0
            public static bool LoadDataFromFile(ref TreeViewSerialize treeViewSerializeOut, ref List <TableGraph> listTableGraphOut, string previosBasePathIn = null)
            {
                bool res = true;

                try
                {
                    string filePath = previosBasePathIn;
                    if (String.IsNullOrWhiteSpace(filePath))
                    {
                        using (OpenFileDialog openFileDialog = new OpenFileDialog())
                        {
                            openFileDialog.Filter           = _fileFilter;
                            openFileDialog.InitialDirectory = Environment.CurrentDirectory;
                            if (openFileDialog.ShowDialog() == DialogResult.OK)
                            {
                                Properties.Settings.Default.PreviousBasePath = openFileDialog.FileName;
                                Properties.Settings.Default.Save();

                                filePath = openFileDialog.FileName;
                            }
                        }
                    }

                    if (!String.IsNullOrWhiteSpace(filePath))
                    {
                        using (StreamReader streamReader = new StreamReader(filePath))
                        {
                            string textFromFile = streamReader.ReadToEnd();
                            SaveLoadDataSerialize dataSerialize = JsonConvert.DeserializeObject <SaveLoadDataSerialize>(textFromFile);

                            treeViewSerializeOut = dataSerialize.TreeViewSerialize;
                            listTableGraphOut    = dataSerialize.TableGraphsList;

                            streamReader.Close();
                        }
                    }
                }
                catch (Exception e)
                {
                    res = false;
                    MessageBox.Show(e.Message, "Ошибка!");
                }

                return(res);
            }