Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Include Simple C# GUI Application
Browse files Browse the repository at this point in the history
  • Loading branch information
Sewer56 committed Sep 19, 2018
1 parent 8a4fa3d commit 90edc9c
Show file tree
Hide file tree
Showing 15 changed files with 816 additions and 161 deletions.
6 changes: 3 additions & 3 deletions csharp-test/App.config → csharp-prs-GUI/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5"/>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
</configuration>
</configuration>
147 changes: 147 additions & 0 deletions csharp-prs-GUI/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

128 changes: 128 additions & 0 deletions csharp-prs-GUI/MainForm.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using csharp_prs;

namespace csharp_prs_GUI
{
public partial class MainForm : Form
{
// Name of the directory relative to application directory to which all files are output.
public const string TempDirectoryName = "temp-output";

public MainForm()
{
InitializeComponent();
}

/*
-----------
Drag Events
-----------
*/

/*
Change Mouse Cursor & Target Drop Effect on Drag Enter with Files
*/
private void btn_CompressDragDrop_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}

private void btn_DecompressDragDrop_DragEnter(object sender, DragEventArgs e)
{
if (e.Data.GetDataPresent(DataFormats.FileDrop))
e.Effect = DragDropEffects.Copy;
}


/*
Process each file in a DragDrop operation.
*/
private void btn_CompressDragDrop_DragDrop(object sender, DragEventArgs e)
{
// Get files dropped onto button and process them.
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
PrepareOutputFolder();

int searchBufferSize = (int)nud_SearchBufferSize.Value;
ParallelOptions options = new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount };
Parallel.ForEach(files, file =>
{
byte[] decompressedFile = File.ReadAllBytes(file);
byte[] compressedFile = Prs.Compress(ref decompressedFile, searchBufferSize);

string fileName = Path.GetFileName(file);
File.WriteAllBytes($"{TempDirectoryName}\\{fileName}", compressedFile);
});

// Open directory
Process.Start(TempDirectoryName);
}

private void btn_DecompressDragDrop_DragDrop(object sender, DragEventArgs e)
{
// Get files dropped onto button and process them.
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
PrepareOutputFolder();

ParallelOptions options = new ParallelOptions() { MaxDegreeOfParallelism = Environment.ProcessorCount };
Parallel.ForEach(files, file =>
{
byte[] compressedFile = File.ReadAllBytes(file);
byte[] decompressedFile = Prs.Decompress(ref compressedFile);

string fileName = Path.GetFileName(file);
File.WriteAllBytes($"{TempDirectoryName}\\{fileName}", decompressedFile);
});

// Open directory
Process.Start(TempDirectoryName);
}

/*
----------------------------
Compress/Decompress Routines
----------------------------
*/

public void PrepareOutputFolder()
{
if (! Directory.Exists(TempDirectoryName))
Directory.CreateDirectory(TempDirectoryName);
else
ClearDirectory(new DirectoryInfo(TempDirectoryName));
}


/// <summary>
/// Removes all files from a specific directory and subdirectories.
/// </summary>
public static void ClearDirectory(DirectoryInfo directory)
{
foreach (FileInfo file in directory.GetFiles())
file.Delete();

foreach (DirectoryInfo subDirectory in directory.GetDirectories())
subDirectory.Delete(true);
}

/// <summary>
/// Cleanup our temp folder.
/// </summary>
private void MainForm_FormClosed(object sender, FormClosedEventArgs e)
{
if (Directory.Exists(TempDirectoryName))
new DirectoryInfo(TempDirectoryName).Delete(true);
}
}
}
Loading

0 comments on commit 90edc9c

Please sign in to comment.