Exemplo n.º 1
0
 private static extern int NtQueryInformationProcess(IntPtr processHandle, int processInformationClass, ref ParentProcessUtilities processInformation, int processInformationLength, out int returnLength);
Exemplo n.º 2
0
        public void doUpdate()
        {
            //Window1 w = new Window1();
            //w.Show();


            //Console.WriteLine(Process.GetProcessById(Process.GetCurrentProcess().Id).Parent().ToString());
            Process parent = ParentProcessUtilities.GetParentProcess();

            if (parent != null && parent.ProcessName != "WearFPSForms" && parent.ProcessName != "devenv")
            {
                Console.WriteLine(parent.ProcessName);
                this.Dispatcher.BeginInvoke(new Action(() =>
                {
                    this.Title      = "Error";
                    image1.Source   = new BitmapImage(new Uri("pack://application:,,,/Updater;component/img/error.png"));
                    textBlock1.Text = "El actualizador no se puede ejecutar directamente";
                }));
                //Console.WriteLine("El actualizador no se puede ejecutar directamente.");
                this.Closing -= new CancelEventHandler(Window_CancelClosing);
                this.Closing += new CancelEventHandler(Window_Closing);
                worker.Abort();
                System.Environment.Exit(0);
                updater.Abort();
                return;
            }

            string parentpath = "";

            if (parent != null)
            {
                parentpath = parent.MainModule.FileName;
                parent.Kill();
                parent.WaitForExit();
#if DEBUG
                Console.WriteLine(parent.ProcessName);
                Console.WriteLine(parent.MainModule.FileName);
                Console.WriteLine(parent.MainModule.BaseAddress);
                Console.WriteLine(parent.MainModule.ModuleName);
#endif
            }
            else
            {
                parentpath = "WearFPS.exe";
            }

            FileStream file;
            Console.WriteLine("ASD");
            try
            {
                file = new FileStream("update-info", FileMode.Open);
            }
            catch
            {
                this.Dispatcher.Invoke(new textUpdater(tU), "Error: no se ha encontrado la información de la actualización");
                //worker.Abort();
                return;
            }
            StreamReader  reader = new StreamReader(file);
            List <string> lines  = new List <string>();
            while (!reader.EndOfStream)
            {
                lines.Add(reader.ReadLine());
            }
            reader.Dispose();
            List <File_> files = new List <File_>(lines.Count);
            foreach (string s in lines)
            {
                Console.WriteLine(s);
                s.Trim();
                if (s[0] == ':')
                {
                }
                else
                {
                    string[] parts = s.Split(';');
                    files.Add(new File_(parts[0], parts[1], parts[2]));
                    Console.WriteLine(parts[0] + "   /   " + parts[1] + "   /   " + parts[2]);
                }
            }
            lines.Clear();
            foreach (File_ f in files)
            {
                if (f.CheckUri())
                {
                    Console.WriteLine(f.Url + " es una URL válida.");
                }
                else
                {
                    Console.WriteLine(f.Url + " NO es una URL válida.");
                    //this.Dispatcher.Invoke(new textUpdater(tU), "Error: no se han encontrado uno o varios archivos");
                    //worker.Abort();
                    //return;
                }
            }

            //this.Dispatcher.BeginInvoke(new textUpdater(pB),0);

            WebClient webClient = new WebClient();
            webClient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(ProgressChanged);
            webClient.DownloadFileCompleted   += new AsyncCompletedEventHandler(DownloadCompleted);

            /*webClient.DownloadFileAsync(new Uri(files[0].Url), @files[0].Path);
             * oThreadReset.WaitOne();
             * oThreadReset.Reset();*/

            foreach (File_ f in files)
            {
                if (f.Exists)
                {
                    this.Dispatcher.Invoke(new textUpdater(titleU), @f.Path);
                    webClient.DownloadFileAsync(new Uri(f.Url), @f.Path);
                    oThreadReset.WaitOne();
                    oThreadReset.Reset();
                    Thread.Sleep(500);
                }
            }
            webClient.Dispose();
            //GC.Collect(1);

            foreach (File_ f in files)
            {
                if (f.Mode == "zip")
                {
                    this.Dispatcher.Invoke(new textUpdater(tU), "Extrayendo " + f.Path + "...");
                    ZipStorer zip = ZipStorer.Open(@f.Path, FileAccess.Read);
                    List <ZipStorer.ZipFileEntry> dir = zip.ReadCentralDir();
                    foreach (ZipStorer.ZipFileEntry entry in dir)
                    {
                        // File found, extract it
                        zip.ExtractFile(entry, @".\" + entry.FilenameInZip);
                    }
                    zip.Close();
                    File.Delete(@f.Path);
                }
            }

            //File.Delete(@"update-info");


            this.Closing -= new CancelEventHandler(Window_CancelClosing);
            this.Closing += new CancelEventHandler(Window_Closing);

            this.Dispatcher.Invoke(new Action(() =>
            {
                this.TaskbarItemInfo.ProgressState = TaskbarItemProgressState.Indeterminate;
                TaskbarItemInfo.Overlay            = new BitmapImage(new Uri("pack://application:,,,/Updater;component/img/load0.png"));
            }));
            Thread.Sleep(1568);
            Process.Start(parentpath);
            System.Environment.Exit(0);
        }