Advertisement
DarkVss

readXML

Jul 6th, 2016
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.43 KB | None | 0 0
  1. class Filer
  2.     {
  3.         static string configPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData).ToString() + "\\Orlan";
  4.         static string configFile = configPath + "\\config.xml";
  5.         static string logFile = configPath + "\\log.txt";
  6.         XDocument configFileReader;
  7.         string URL = null;
  8.         string softKey = null;
  9.         string apiKey = null;
  10.  
  11.         public Filer()
  12.         {
  13.             try
  14.             {
  15.                 if (!Directory.Exists(configPath))
  16.                 {
  17.                     Directory.CreateDirectory(configPath);
  18.                 }
  19.                 if (!File.Exists(configFile))
  20.                 {
  21.                     File.Create(configFile);
  22.                 }
  23.                 try
  24.                 {
  25.                     configFileReader = XDocument.Load(configFile);
  26.                 }
  27.                 catch
  28.                 {
  29.                     while (configFileReader == null)
  30.                     {
  31.                         File.Create(configFile);
  32.                         configFileReader =
  33.                           new XDocument(new XDeclaration("1.0", "utf8", "yes"),
  34.                             new XElement("root",
  35.                               new XElement("url"),
  36.                               new XElement("softKey"),
  37.                               new XElement("apiKey")
  38.                             )
  39.                           );
  40.                         //configFileReader.Save(configFile);
  41.                     }
  42.  
  43.                 }
  44.  
  45.                 if (configFileReader.Root.Element("url") != null)
  46.                 {
  47.                     URL = configFileReader.Root.Element("url").ToString();
  48.                 }
  49.                 else
  50.                 {
  51.                     configFileReader.Root.Add("url");
  52.                 }
  53.                 if (configFileReader.Root.Element("softKey") != null)
  54.                 {
  55.                     softKey = configFileReader.Root.Element("softKey").ToString();
  56.                 }
  57.                 if (configFileReader.Root.Element("apiKey") != null)
  58.                 {
  59.                     apiKey = configFileReader.Root.Element("apiKey").ToString();
  60.                 }
  61.             }
  62.             catch (Exception e)
  63.             {
  64.                 this.setLog(e.ToString());
  65.             }
  66.         }
  67.  
  68.         public string newConfig(string url, string sK, string aK)
  69.         {
  70.             string result;
  71.             if (url.Length == 0)
  72.             {
  73.                 result = "Задан пустой адрес обращения!";
  74.             }
  75.             else if (sK.Length == 0)
  76.             {
  77.                 result = "Задан пустой soft_key!";
  78.             }
  79.             else if (aK.Length == 0)
  80.             {
  81.                 result = "Задан пустой api_key!";
  82.             }
  83.             else
  84.             {
  85.                 configFileReader.Root.Element("url").Value = url;
  86.                 configFileReader.Root.Element("softKey").Value = sK;
  87.                 configFileReader.Root.Element("apiKey").Value = aK;
  88.                 configFileReader.Save(configFile);
  89.                 result = "Данные для подключения сохранены.";
  90.             }
  91.             return result;
  92.         }
  93.  
  94.         public void setLog(string exception)
  95.         {
  96.             File.AppendAllText(logFile, exception + Environment.NewLine + "===================" + Environment.NewLine);
  97.         }
  98.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement