using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.IO;
using IWshRuntimeLibrary;
using Microsoft.Win32;
namespace StoneUpdater.Class
{
    class Updater
    {


       public string XmlDataFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" +"\\Updater"+ "\\Update.xml";
       public string XmlSchemaFileName = System.Environment.GetFolderPath(System.Environment.SpecialFolder.ApplicationData) + "\\ParsZSDB" + "\\Updater" + "\\Update.xsd";

       public DataTable Update = new DataTable("Update");

       public Updater()
       {
           #region Create_Update

           DataColumn Operation = new DataColumn("Operation", System.Type.GetType("System.String"));
           DataColumn Name = new DataColumn("Name", System.Type.GetType("System.String"));
           DataColumn Value = new DataColumn("Value", System.Type.GetType("System.String"));
           DataColumn Target1 = new DataColumn("Target1", System.Type.GetType("System.String"));
           DataColumn Target2 = new DataColumn("Target2", System.Type.GetType("System.String"));

           Update.Columns.Add(Operation);
           Update.Columns.Add(Name);
           Update.Columns.Add(Value);
           Update.Columns.Add(Target1);
           Update.Columns.Add(Target2);


           #endregion

           #region Read_XMLfile

               Update.ReadXmlSchema(XmlSchemaFileName);
               Update.ReadXml(XmlDataFileName);

           #endregion
       }

       public void CreateShortcut(string destination, string filenameEx , string IcoFile)
       {
           WshShell wshShell = new WshShell();
           object destFolder = (object)destination;
           string fileName = Path.Combine(
               (string)wshShell.SpecialFolders.Item(ref destFolder),filenameEx+
               ".lnk"
           );

           IWshShortcut shortcut =
               (IWshShortcut)wshShell.CreateShortcut(fileName);
           shortcut.TargetPath = Path.Combine(
               System.Windows.Forms.Application.StartupPath,filenameEx+
               ".exe"
           );
           shortcut.WorkingDirectory =
                System.Windows.Forms.Application.StartupPath;

           shortcut.IconLocation = System.Windows.Forms.Application.StartupPath +"\\"+ IcoFile;
           shortcut.Save();
       }



       public void Create_key_Registery(string strpathreg , string name , string value)
       {
           object key = Registry.CurrentUser.GetValue(strpathreg);
           if (key == null)
           {
               RegistryKey mm = Registry.CurrentUser.CreateSubKey(strpathreg , RegistryKeyPermissionCheck.ReadWriteSubTree , RegistryOptions.Volatile);
               mm.SetValue(name, value);
               mm.Close();
           }
       }

       public void Delete_key_Registery(string strpathreg, string name, string value)
       {
           try
           {
               Registry.CurrentUser.OpenSubKey(strpathreg, true).DeleteValue(name , true);
           }
           catch (Exception ex)
           {
           }
       }


       public void DeleteUpdatePackage(DirectoryInfo source)
       {

           if (source.Exists)
           {
               FileInfo[] files = source.GetFiles();
               foreach (FileInfo file1 in files)

                   file1.Delete();

               source.Delete();

           }

       }


    }
}
