Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Add-Type -AssemblyName System.Windows.Forms
- # Définir le chemin du fichier de configuration JSON
- $configFile = "config.json"
- # Fonction pour sauvegarder les paramètres dans un fichier JSON
- function SaveToFile($filePathSearch, $filePathExecute, $isAutoSearchChecked) {
- $jsonObject = @{
- "FilePathSearch" = $filePathSearch
- "FilePathExecute" = $filePathExecute
- "IsAutoSearchChecked" = $isAutoSearchChecked
- } | ConvertTo-Json
- $jsonObject | Out-File -FilePath $configFile -Encoding UTF8
- }
- # Fonction pour charger les paramètres depuis le fichier JSON
- function LoadFileSelection() {
- if (Test-Path $configFile) {
- $jsonObject = Get-Content -Path $configFile | ConvertFrom-Json
- $filePathSearch = $jsonObject.FilePathSearch
- $filePathExecute = $jsonObject.FilePathExecute
- $isAutoSearchChecked = $jsonObject.IsAutoSearchChecked
- return $filePathSearch, $filePathExecute, $isAutoSearchChecked
- }
- return $null, $null, $null
- }
- # Fonction pour vérifier si un processus est en cours d'exécution
- function CheckProcessRunning($processName) {
- $process = Get-Process -Name $processName -ErrorAction SilentlyContinue
- return $process -ne $null
- }
- # Fonction pour lancer le fichier sélectionné
- function StartSelectedFile($filePathExecute) {
- if ($filePathExecute -and (Test-Path $filePathExecute)) {
- $folderPath = Split-Path -Parent $filePathExecute
- cd\
- cd $folderPath
- start $filePathExecute
- Write-Host "Le fichier a été exécuté."
- } else {
- [System.Windows.Forms.MessageBox]::Show("Veuillez sélectionner un fichier à exécuter valide.", "Erreur", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error)
- }
- }
- # Crée un formulaire
- $form = New-Object Windows.Forms.Form
- $form.Text = "Gestion de GTA 5"
- $form.Size = New-Object Drawing.Size(600, 400)
- $form.StartPosition = "CenterScreen"
- # Crée une case à cocher pour sélectionner le fichier .exe à rechercher dans Tasklist
- $checkBoxSelectFileSearch = New-Object Windows.Forms.CheckBox
- $checkBoxSelectFileSearch.Text = "Rechercher le fichier .exe dans Tasklist"
- $checkBoxSelectFileSearch.Location = New-Object Drawing.Point(20, 20)
- # Crée une case à cocher pour sélectionner le fichier à exécuter
- $checkBoxSelectFileExecute = New-Object Windows.Forms.CheckBox
- $checkBoxSelectFileExecute.Text = "Sélectionner le fichier à exécuter"
- $checkBoxSelectFileExecute.Location = New-Object Drawing.Point(20, 50)
- # Crée une case à cocher pour activer/désactiver la recherche automatique
- $checkBoxAutoSearch = New-Object Windows.Forms.CheckBox
- $checkBoxAutoSearch.Text = "Recherche automatique avec démarrage automatique"
- $checkBoxAutoSearch.Location = New-Object Drawing.Point(20, 80)
- # Charge les chemins des fichiers sélectionnés et l'état de recherche depuis le fichier JSON
- $filePathSearch, $filePathExecute, $isAutoSearchChecked = LoadFileSelection
- $checkBoxSelectFileSearch.Checked = ($filePathSearch -ne $null)
- $checkBoxSelectFileExecute.Checked = ($filePathExecute -ne $null)
- $checkBoxAutoSearch.Checked = $isAutoSearchChecked
- $form.Controls.Add($checkBoxSelectFileSearch)
- $form.Controls.Add($checkBoxSelectFileExecute)
- $form.Controls.Add($checkBoxAutoSearch)
- # Crée un champ de texte pour afficher/écrire le chemin du fichier .exe à rechercher
- $textBoxFilePathSearch = New-Object Windows.Forms.TextBox
- $textBoxFilePathSearch.Location = New-Object Drawing.Point(20, 120)
- $textBoxFilePathSearch.Width = 400
- $form.Controls.Add($textBoxFilePathSearch)
- # Crée un champ de texte pour afficher/écrire le chemin du fichier à exécuter
- $textBoxFilePathExecute = New-Object Windows.Forms.TextBox
- $textBoxFilePathExecute.Location = New-Object Drawing.Point(20, 150)
- $textBoxFilePathExecute.Width = 400
- $form.Controls.Add($textBoxFilePathExecute)
- # Crée un bouton pour démarrer le jeu
- $buttonStart = New-Object Windows.Forms.Button
- $buttonStart.Text = "Démarrer"
- $buttonStart.Location = New-Object Drawing.Point(20, 190)
- $form.Controls.Add($buttonStart)
- # Crée un label pour afficher le temps restant du minuteur
- $labelTimer = New-Object Windows.Forms.Label
- $labelTimer.Text = "Temps restant : 120 secondes"
- $labelTimer.Location = New-Object Drawing.Point(20, 220)
- $form.Controls.Add($labelTimer)
- # Crée un minuteur pour vérifier périodiquement si le processus est en cours d'exécution
- $timer = New-Object System.Windows.Forms.Timer
- $timer.Interval = 1000 # 1 seconde
- $currentTimerCount = 120
- $form.Add_Shown({
- $currentTimerCount = 120
- $timer.Start()
- })
- # Gère l'événement Tick du minuteur pour vérifier le processus et lancer le fichier sélectionné
- $timer.Add_Tick({
- if ($checkBoxAutoSearch.Checked) {
- if ($filePathSearch -and (Test-Path $filePathSearch)) {
- $isProcessRunning = CheckProcessRunning (Get-Item $filePathSearch).BaseName
- if (-not $isProcessRunning) {
- if ($filePathExecute -and (Test-Path $filePathExecute)) {
- $buttonStart.Hide() # Cacher le bouton de démarrage
- # Décompte de 120 secondes avant de lancer le fichier sélectionné
- $currentTimerCount = 120 # Réinitialiser le décompte à 120 secondes
- while ($currentTimerCount -gt 0) {
- $labelTimer.Text = "Temps restant : $currentTimerCount secondes"
- Start-Sleep -Seconds 1
- $currentTimerCount--
- }
- StartSelectedFile $filePathExecute
- $buttonStart.Show() # Afficher le bouton de démarrage après le lancement
- }
- }
- }
- }
- })
- # Gère l'événement Click du bouton "Démarrer"
- $buttonStart.Add_Click({
- StartSelectedFile $filePathExecute
- })
- # Gère l'événement CheckedChanged de la case à cocher pour afficher le OpenFileDialog pour le fichier à rechercher
- $checkBoxSelectFileSearch.Add_CheckedChanged({
- $textBoxFilePathSearch.Enabled = $checkBoxSelectFileSearch.Checked
- if ($checkBoxSelectFileSearch.Checked) {
- $openFileDialogSearch = New-Object Windows.Forms.OpenFileDialog
- $openFileDialogSearch.Title = "Sélectionner le fichier à rechercher"
- $openFileDialogSearch.Filter = "Exécutables (*.exe)|*.exe|Tous les fichiers (*.*)|*.*"
- if ($openFileDialogSearch.ShowDialog() -eq [Windows.Forms.DialogResult]::OK) {
- $filePathSearch = $openFileDialogSearch.FileName
- $textBoxFilePathSearch.Text = $filePathSearch
- # Sauvegarder l'état de la case à cocher dans le fichier JSON
- SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
- }
- } else {
- $filePathSearch = $null
- $textBoxFilePathSearch.Text = ""
- # Sauvegarder l'état de la case à cocher dans le fichier JSON
- SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
- }
- })
- # Gère l'événement CheckedChanged de la case à cocher pour afficher le OpenFileDialog pour le fichier à exécuter
- $checkBoxSelectFileExecute.Add_CheckedChanged({
- $textBoxFilePathExecute.Enabled = $checkBoxSelectFileExecute.Checked
- if ($checkBoxSelectFileExecute.Checked) {
- $openFileDialogExecute = New-Object Windows.Forms.OpenFileDialog
- $openFileDialogExecute.Title = "Sélectionner le fichier à exécuter"
- $openFileDialogExecute.Filter = "Exécutables (*.exe)|*.exe|Tous les fichiers (*.*)|*.*"
- if ($openFileDialogExecute.ShowDialog() -eq [Windows.Forms.DialogResult]::OK) {
- $filePathExecute = $openFileDialogExecute.FileName
- $textBoxFilePathExecute.Text = $filePathExecute
- # Sauvegarder l'état de la case à cocher dans le fichier JSON
- SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
- }
- } else {
- $filePathExecute = $null
- $textBoxFilePathExecute.Text = ""
- # Sauvegarder l'état de la case à cocher dans le fichier JSON
- SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
- }
- })
- # Gère l'événement CheckedChanged de la case à cocher pour la recherche automatique
- $checkBoxAutoSearch.Add_CheckedChanged({
- # Sauvegarder l'état de la case à cocher dans le fichier JSON
- SaveToFile $filePathSearch $filePathExecute $checkBoxAutoSearch.Checked
- if ($checkBoxAutoSearch.Checked) {
- # Cacher le bouton "Démarrer" lorsque la recherche automatique est activée
- $buttonStart.Hide()
- # Vérifier et lancer le fichier sélectionné automatiquement si la case "Recherche automatique" est cochée
- $timer.Start()
- } else {
- # Afficher le bouton "Démarrer" lorsque la recherche automatique est désactivée
- $buttonStart.Show()
- # Arrêter la vérification automatique si la case "Recherche automatique" est désactivée
- $timer.Stop()
- }
- })
- # Ajouter ceci pour arrêter le minuteur lorsque la case à cocher est décochée
- $checkBoxAutoSearch.Add_CheckedChanged({
- if (-not $checkBoxAutoSearch.Checked) {
- $timer.Stop()
- }
- })
- # Mettre à jour l'affichage initial des contrôles en fonction de l'état des cases à cocher
- $textBoxFilePathSearch.Text = $filePathSearch
- $textBoxFilePathSearch.Enabled = $checkBoxSelectFileSearch.Checked
- $textBoxFilePathExecute.Text = $filePathExecute
- $textBoxFilePathExecute.Enabled = $checkBoxSelectFileExecute.Checked
- # Charger le formulaire
- $form.ShowDialog() | Out-Null
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement