Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
The Agent - frmMacro.vb

frmMacro.vb

Caricato da: Totem
Scarica il programma completo

  1. Imports System.IO
  2. Imports Microsoft.Win32
  3. Public Class frmMacro
  4.     'Carica tutte le macro salvate
  5.     Public Sub RefreshList()
  6.         If Directory.Exists(Application.StartupPath + "\Macro") Then
  7.             Dim Files() As String = Directory.GetFiles(Application.StartupPath + "\Macro")
  8.             Array.Sort(Files)
  9.             lstMacros.Items.Clear()
  10.             For I As Int16 = 0 To UBound(Files)
  11.                 lstMacros.Items.Add(Path.GetFileNameWithoutExtension(Files(I)))
  12.             Next
  13.         Else
  14.             Directory.CreateDirectory(Application.StartupPath + "\Macro")
  15.         End If
  16.     End Sub
  17.  
  18.     'Carica tutte le macro salvate
  19.     Private Sub frmMacro_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  20.         RefreshList()
  21.     End Sub
  22.  
  23.     'Quando si seleziona un elemento, lo carica
  24.     Private Sub lstMacros_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMacros.SelectedIndexChanged
  25.         If lstMacros.SelectedIndex >= 0 Then
  26.             Try
  27.                 Dim R As New StreamReader(Application.StartupPath + "\Macro\" + lstMacros.SelectedItem.ToString + ".bat")
  28.                 txtMacro.Text = R.ReadToEnd
  29.                 R.Close()
  30.                 txtName.Text = Path.GetFileNameWithoutExtension(Application.StartupPath + "\Macro\" + lstMacros.SelectedItem.ToString + ".bat")
  31.             Catch Ex As Exception
  32.                 MessageBox.Show("Si è verificato un errore nell'apertura del file!", "Error #1", MessageBoxButtons.OK, MessageBoxIcon.Information)
  33.             End Try
  34.         End If
  35.     End Sub
  36.  
  37.     'Aggiunge al testo il codice per eseguire un programma
  38.     Private Sub strExecProg_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strExecProg.Click
  39.         Dim O As New OpenFileDialog
  40.         O.Filter = "Applicazioni, eseguibili|*.exe;*.com;*.msi;*.scr"
  41.         If O.ShowDialog = Windows.Forms.DialogResult.OK Then
  42.             txtMacro.Text += "REM Esegue " + Path.GetFileName(O.FileName) + vbCrLf
  43.             txtMacro.Text += Chr(34) + O.FileName + Chr(34) + vbCrLf + vbCrLf
  44.         End If
  45.     End Sub
  46.  
  47.     'Aggiunge al testo il codice per aprire una cartella
  48.     Private Sub strOpenFolder_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strOpenFolder.Click
  49.         Dim F As New FolderBrowserDialog
  50.         F.Description = "Scegliere quale cartella aprire:"
  51.         If F.ShowDialog = Windows.Forms.DialogResult.OK Then
  52.             txtMacro.Text += "REM Apre la cartella " + Path.GetFileName(F.SelectedPath) + vbCrLf
  53.             txtMacro.Text = "C:\WINDOWS\explorer.exe " + Chr(34) + F.SelectedPath + Chr(34) + vbCrLf + vbCrLf
  54.         End If
  55.     End Sub
  56.  
  57.     'Aggiunge al testo il codice per aprire un file
  58.     Private Sub strOpenFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles strOpenFile.Click
  59.         Dim O As New OpenFileDialog
  60.         O.Filter = "Qualsiasi file|*.*"
  61.         If O.ShowDialog = Windows.Forms.DialogResult.OK Then
  62.             Dim RegKey As RegistryKey
  63.             Dim Ext As String = Path.GetExtension(O.FileName)
  64.             Dim Key As String
  65.  
  66.             'Cerca la chiave associata all'estensione nel registro
  67.             RegKey = Registry.ClassesRoot.OpenSubKey(Ext)
  68.             If RegKey Is Nothing Then
  69.                 MessageBox.Show("L'estensione non è contemplata all'interno del registro di sistema!", "Error #2", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  70.                 Exit Sub
  71.             End If
  72.             Key = RegKey.GetValue("")
  73.  
  74.             RegKey = Registry.ClassesRoot.OpenSubKey(Key)
  75.             RegKey = RegKey.OpenSubKey("shell\open\command")
  76.             If RegKey Is Nothing Then
  77.                 MessageBox.Show("Nessuna applicazione associata a tale estensione!", "Error #3", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  78.                 Exit Sub
  79.             End If
  80.             Key = RegKey.GetValue("")
  81.             Key = Key.Replace("%L", O.FileName)
  82.             Key = Key.Replace("%1", O.FileName)
  83.  
  84.             txtMacro.Text += "REM Apre il file " + Path.GetFileName(O.FileName) + vbCrLf
  85.             txtMacro.Text += Key + vbCrLf + vbCrLf
  86.         End If
  87.     End Sub
  88.  
  89.     'Cancella la macro
  90.     Private Sub cmdDel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdDel.Click
  91.         If lstMacros.SelectedIndex >= 0 Then
  92.             Try
  93.                 File.Delete(Application.StartupPath + "\Macro\" + lstMacros.SelectedItem.ToString + ".bat")
  94.                 lstMacros.Items.RemoveAt(lstMacros.SelectedIndex)
  95.             Catch Ex As Exception
  96.                 MessageBox.Show("Errore nella cancellazione del file!", "Error #4", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  97.             End Try
  98.         End If
  99.     End Sub
  100.  
  101.     'Salva il file
  102.     Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
  103.         If txtMacro.Text = Nothing Or txtName.Text = Nothing Then
  104.             MessageBox.Show("Compilare i campi necessari!", "Error #5", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
  105.             Exit Sub
  106.         End If
  107.  
  108.         Dim W As New StreamWriter(Application.StartupPath + "\Macro\" + txtName.Text + ".bat")
  109.         W.Write(txtMacro.Text)
  110.         W.Close()
  111.  
  112.         RefreshList()
  113.     End Sub
  114.  
  115.     'Esegue la macro su cui si è fatto doppio click
  116.     Private Sub lstMacros_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lstMacros.DoubleClick
  117.         If lstMacros.SelectedIndex >= 0 Then
  118.             Dim File As String = Application.StartupPath + "\Macro\" + lstMacros.SelectedItem.ToString + ".bat"
  119.             Shell(File)
  120.         End If
  121.     End Sub
  122. End Class