-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
128 lines (108 loc) · 4.04 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
using System.Diagnostics;
using System.IO;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
using System.Windows.Forms;
using System;
using System.Globalization;
using System.IO.Compression;
using System.Threading;
using System.Configuration;
namespace Resizator
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string batchFilePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "resize.bat");
private readonly Settings settings = Settings.Load();
public MainWindow()
{
InitializeComponent();
Closing += MainWindowClosing;
tbxPercent.Text = settings.Percent;
tbxPath.Text = settings.Path;
}
private void MainWindowClosing(object? sender, System.ComponentModel.CancelEventArgs e)
{
settings.Percent = tbxPercent.Text;
settings.Path = tbxPath.Text;
settings.Save();
}
private void OpenFileDialog(object sender, RoutedEventArgs e)
{
using var fbd = new FolderBrowserDialog();
fbd.RootFolder = Environment.SpecialFolder.MyDocuments;
if(fbd.ShowDialog() == System.Windows.Forms.DialogResult.OK )
{
tbxPath.Text = fbd.SelectedPath;
}
}
private void Resize(object sender, RoutedEventArgs e)
{
try
{
tbxConsole.Clear();
decimal percent = decimal.Parse(tbxPercent.Text) / 100;
// Set up the process start info
ProcessStartInfo processStartInfo = new()
{
CreateNoWindow = true,
FileName = $"cmd.exe",
Arguments = $" /C \"\"{batchFilePath}\" {percent.ToString("0.00", CultureInfo.InvariantCulture)} \"{tbxPath.Text}\"\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
};
Debug.WriteLine(processStartInfo.Arguments);
// Start the process
using var process = Process.Start(processStartInfo);
process.EnableRaisingEvents = true;
process.OutputDataReceived += ProcessOutput;
process.ErrorDataReceived += ProcessOutput;
process.BeginErrorReadLine();
process.BeginOutputReadLine();
// Clean up
process.WaitForExit();
process.Close();
Compress();
WriteToConsole("Done !");
}
catch (Exception ex)
{
tbxConsole.Text += $"ERROR: {ex.Message}\n";
}
}
private void Compress()
{
string outputFolder = Path.Combine(tbxPath.Text, "resized");
if (Directory.Exists(outputFolder))
{
string zipPath = Path.Combine(tbxPath.Text, "resized.zip");
WriteToConsole($"Compressing \"{outputFolder}\" into \"{zipPath}\"...");
if (File.Exists(zipPath))
{
File.Delete(zipPath);
}
ZipFile.CreateFromDirectory(outputFolder, zipPath);
WriteToConsole($"Deleting \"{outputFolder}\"...");
Directory.Delete(outputFolder, true);
}
}
private void ProcessOutput(object sender, DataReceivedEventArgs e)
{
if(e.Data != null)
{
WriteToConsole(e.Data);
}
}
private void WriteToConsole(string txt) => tbxConsole.Dispatcher.BeginInvoke(() => { tbxConsole.Text += txt + "\n"; });
private void ValidatePercent(object sender, TextCompositionEventArgs e)
{
var newValue = tbxPercent.Text + e.Text;
e.Handled = !Regex.IsMatch(newValue, @"^[1-9][0-9]?$");
}
}
}