0% found this document useful (0 votes)
12 views5 pages

Using System

The document contains a C# code implementation for a file compression application using Windows Forms. It includes methods for selecting files, compressing them into a ZIP format, ensuring directory existence, and displaying operation details. Additionally, it provides functionality for managing child forms within the application interface.

Uploaded by

Gustavo 2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views5 pages

Using System

The document contains a C# code implementation for a file compression application using Windows Forms. It includes methods for selecting files, compressing them into a ZIP format, ensuring directory existence, and displaying operation details. Additionally, it provides functionality for managing child forms within the application interface.

Uploaded by

Gustavo 2005
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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();
}
}

You might also like