3.
1
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Windows.Forms;
public class FileCompressor
{
public static void CompressFiles(string[] files, string zipPath)
{
if (files == null || files.Length == 0)
{
MessageBox.Show("Nenhum ficheiro selecionado!", "Erro",
MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
try
{
using (ZipArchive zip = ZipFile.Open(zipPath, ZipArchiveMode.Create))
{
foreach (string file in files)
{
zip.CreateEntryFromFile(file, Path.GetFileName(file),
CompressionLevel.Optimal);
}
}
MessageBox.Show("Ficheiros comprimidos com sucesso!", "Sucesso",
MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Erro ao comprimir: {ex.Message}", "Erro",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
3.2
private void btnSelecionarFicheiros_Click(object sender, EventArgs e)
{
using (OpenFileDialog openFileDialog = new OpenFileDialog())
{
openFileDialog.Multiselect = true;
if (openFileDialog.ShowDialog() == DialogResult.OK)
{
txtFicheirosSelecionados.Text = string.Join(";", openFileDialog.FileNames);
}
}
}
private void btnSelecionarPastaDestino_Click(object sender, EventArgs e)
{
using (FolderBrowserDialog folderDialog = new FolderBrowserDialog())
{
if (folderDialog.ShowDialog() == DialogResult.OK)
{
txtPastaDestino.Text = folderDialog.SelectedPath;
}
}
}
private void btnComprimir_Click(object sender, EventArgs e)
{
string[] files = txtFicheirosSelecionados.Text.Split(';');
string zipName = txtNomeZip.Text.Trim();
string directory = txtPastaDestino.Text.Trim();
if (string.IsNullOrWhiteSpace(zipName) || string.IsNullOrWhiteSpace(directory))
{
MessageBox.Show("Informe um nome para o ZIP e uma pasta de destino!",
"Erro", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string zipPath = Path.Combine(directory, zipName + ".zip");
FileCompressor.CompressFiles(files, zipPath);
}
3.3
private void EnsureDirectoryExists(string directory)
{
if (!Directory.Exists(directory))
{
Directory.CreateDirectory(directory);
}
}
3.4
private void AdicionarRegistro(string usuario, string zipName, string directory, int
fileCount)
{
dgvListaB.Rows.Add(usuario, DateTime.Now.ToString("dd/MM/yyyy
HH:mm:ss"), fileCount, zipName, directory);
}
3.5
private void btnVisualizarDetalhes_Click(object sender, EventArgs e)
{
MessageBox.Show("Detalhes das operações realizadas:\n" +
string.Join("\n", dgvListaB.Rows.Cast<DataGridViewRow>()
.Select(row => $"{row.Cells[0].Value} - {row.Cells[1].Value} -
{row.Cells[3].Value}")),
"Detalhes", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
4.1
private void btnFechar_Click(object sender, EventArgs e)
{
this.Close();
contextMenuStrip1.Hide();
}
4.2
private Form ObterChildFormExistente(Type formType)
{
foreach (Form form in Application.OpenForms)
{
if (form.GetType() == formType)
{
return form;
}
}
return null;
}
private void btnAbrirChildForm_Click(object sender, EventArgs
e)
{
Form existingForm =
ObterChildFormExistente(typeof(ChildForm));
if (existingForm != null)
{
existingForm.Focus();
}
else
{
ChildForm child = new ChildForm();
child.Show();
}
}