Using System
Using System
1
using System;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Windows.Forms;
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);
}
}
}
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;
}
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;
}
if (existingForm != null)
{
existingForm.Focus();
}
else
{
ChildForm child = new ChildForm();
child.Show();
}
}