Scorpunduk

SampleFileAccess

Feb 11th, 2022 (edited)
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 7.56 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Xml.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Windows;
  8. using System.Windows.Controls;
  9. using System.Windows.Data;
  10. using System.Windows.Documents;
  11. using System.Windows.Input;
  12. using System.Windows.Media;
  13. using System.Windows.Media.Imaging;
  14. using System.Windows.Navigation;
  15. using System.Windows.Shapes;
  16. using System.Reflection;
  17. using System.IO;
  18. using Microsoft.Win32;
  19.  
  20. namespace CalcTables
  21. {
  22.     /// <summary>
  23.     /// Логика взаимодействия для ModelPage.xaml
  24.     /// </summary>
  25.     public partial class ModelPage : UserControl
  26.     {
  27.         public ModelPage()
  28.         {
  29.             InitializeComponent();
  30.             stubText.Text = this.GetType().Name;
  31.         }
  32.  
  33.         public Action ReturnToTabView;
  34.         public Action LoadState;
  35.         public Action<string> SaveState;
  36.  
  37.         private void returnToTabView_Click(object sender, RoutedEventArgs e)
  38.         {
  39.             ReturnToTabView.Invoke();
  40.         }
  41.  
  42.         private void Load_Click(object sender, RoutedEventArgs e)
  43.         {
  44.             OpenFileDialog fileDialog = new OpenFileDialog();
  45.             fileDialog.Filter = ((App)App.Current).SupportedExtensions;
  46.             fileDialog.InitialDirectory = Directory.GetCurrentDirectory();
  47.             fileDialog.CheckFileExists = true;
  48.             fileDialog.CheckPathExists = true;
  49.             fileDialog.Multiselect = false;
  50.             fileDialog.DefaultExt = fileDialog.Filter.Split('|')[1];
  51.  
  52.             if (fileDialog.ShowDialog() == true)
  53.             {
  54.                 foreach (var win in App.Current.Windows)
  55.                 {
  56.                     if (win is TabPanelWindow)
  57.                     {
  58.                         var downcast = (TabPanelWindow)win;
  59.                         foreach (var kvp in downcast.TabInvokerType)
  60.                         {
  61.                             kvp.Key.IsChecked = false;
  62.                         }
  63.  
  64.                         downcast.tabPanel.tabContainer.Children.Clear();
  65.                         downcast.mainGrid.Children.Remove(downcast.ActivePage);
  66.                         downcast.ActivePage = null;
  67.                         if (downcast.IsMainWindow) continue;
  68.                         downcast.Close();
  69.                     }
  70.                 }
  71.  
  72.                 foreach (var item in App.TabList)
  73.                 {
  74.                     item.Tab = null;
  75.                     if (item.WindowPage is ICommonControlService)
  76.                     {
  77.                         (item.WindowPage as ICommonControlService).DetachSideControls();
  78.                     }
  79.                     item.WindowPage = null;
  80.                 }
  81.  
  82.  
  83.                 App.modelPath = fileDialog.FileName;
  84.                 ((App)App.Current).OnLoadModel.Invoke(App.modelPath, (string modelGuid) =>
  85.                 {
  86.  
  87.                     FileInfo modelFileInfo = new FileInfo(App.modelPath);
  88.                     string xmlFileName = modelFileInfo.FullName.Replace(modelFileInfo.Extension, ".smcfg");
  89.                     FileInfo xmlFileInfo = new FileInfo(xmlFileName);
  90.  
  91.                     if (xmlFileInfo.Exists)
  92.                     {
  93.                         XDocument xmlDoc = XDocument.Load(xmlFileInfo.FullName);
  94.                         bool hasModelGuidNode = xmlDoc.Descendants()
  95.                             .Where(node => node.Name == "ModelGUID")
  96.                             .Count() > 0;
  97.                         App.appConfigPath = xmlFileInfo.FullName;
  98.                         LoadState.Invoke();
  99.                         ((App)App.Current).AfterLoad.Invoke();
  100.                     }
  101.                     else
  102.                     {
  103.                         DirectoryInfo modelDirectory = modelFileInfo.Directory;
  104.                         var allConfigFiles = modelDirectory.GetFiles()
  105.                             .Where(file => file.Extension == ".smcfg");
  106.  
  107.                         foreach (var file in allConfigFiles)
  108.                         {
  109.                             XDocument configDoc = XDocument.Load(file.FullName);
  110.                             var modelGuidValues = configDoc.Descendants()
  111.                                 .Where(node => node.Name == "ModelGUID")
  112.                                 .Select(node => { return node.Value; });
  113.                             if (modelGuidValues.Count() > 0 && modelGuidValues.FirstOrDefault() == modelGuid)
  114.                             {
  115.                                 App.appConfigPath = file.FullName;
  116.                                 LoadState.Invoke();
  117.                                 ((App)App.Current).AfterLoad.Invoke();
  118.                             }
  119.                         }
  120.                     }
  121.                 });
  122.             }
  123.         }
  124.  
  125.         private void Save_Click(object sender, RoutedEventArgs e)
  126.         {
  127.             SaveFileDialog fileDialog = new SaveFileDialog();
  128.             fileDialog.Filter = ((App)App.Current).SupportedExtensions;
  129.             fileDialog.InitialDirectory = Directory.GetCurrentDirectory();
  130.             fileDialog.OverwritePrompt = true;
  131.             fileDialog.ValidateNames = true;
  132.             fileDialog.DefaultExt = fileDialog.Filter.Split('|')[1];
  133.  
  134.             if (fileDialog.ShowDialog() == true)
  135.             {
  136.                 App.modelPath = fileDialog.FileName;
  137.                 ((App)App.Current).OnSaveModel.Invoke(App.modelPath, (string modelGuid) =>
  138.                 {
  139.  
  140.                     FileInfo modelFileInfo = new FileInfo(App.modelPath);
  141.                     string xmlFileName = modelFileInfo.FullName.Replace(modelFileInfo.Extension, ".smcfg");
  142.                     FileInfo xmlFileInfo = new FileInfo(xmlFileName);
  143.  
  144.                     if (xmlFileInfo.Exists)
  145.                     {
  146.                         XDocument xmlDoc = XDocument.Load(xmlFileInfo.FullName);
  147.                         bool hasModelGuidNode = xmlDoc.Descendants()
  148.                             .Where(node => node.Name == "ModelGUID")
  149.                             .Count() > 0;
  150.                         App.appConfigPath = xmlFileInfo.FullName;
  151.                         SaveState.Invoke(modelGuid);
  152.                     }
  153.                     else
  154.                     {
  155.                         DirectoryInfo modelDirectory = modelFileInfo.Directory;
  156.                         var allConfigFiles = modelDirectory.GetFiles()
  157.                             .Where(file => file.Extension == ".smcfg");
  158.  
  159.                         foreach (var file in allConfigFiles)
  160.                         {
  161.                             XDocument configDoc = XDocument.Load(file.FullName);
  162.                             var modelGuidValues = configDoc.Descendants()
  163.                                 .Where(node => node.Name == "ModelGUID")
  164.                                 .Select(node => { return node.Value; });
  165.                             if (modelGuidValues.Count() > 0 && modelGuidValues.FirstOrDefault() == modelGuid)
  166.                             {
  167.                                 App.appConfigPath = file.FullName;
  168.                                 SaveState.Invoke(modelGuid);
  169.                                 return;
  170.                             }
  171.                         }
  172.                         FileStream createdXml = xmlFileInfo.Create();
  173.                         createdXml.Close();
  174.                         App.appConfigPath = xmlFileInfo.FullName;
  175.                         SaveState.Invoke(modelGuid);
  176.                     }
  177.                 });
  178.             }
  179.         }
  180.  
  181.         public bool IsActive
  182.         {
  183.             get;
  184.             set;
  185.         }
  186.     }
  187. }
Add Comment
Please, Sign In to add comment