-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathMainForm.cs
More file actions
208 lines (170 loc) · 7.41 KB
/
MainForm.cs
File metadata and controls
208 lines (170 loc) · 7.41 KB
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Reflection;
using System.Windows.Forms;
namespace CabMaker
{
public partial class MainForm : Form
{
private const int EXIT_CODE_SUCCESS = 0;
public MainForm()
{
InitializeComponent();
}
private void btnSourceBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.SelectedPath = txtSourceFolder.Text;
if (dialog.ShowDialog() == DialogResult.OK)
{
txtSourceFolder.Text = dialog.SelectedPath;
}
}
private void btnTargetBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.SelectedPath = txtTargetFolder.Text;
if (dialog.ShowDialog() == DialogResult.OK)
{
txtTargetFolder.Text = dialog.SelectedPath;
}
}
private void btnRun_Click(object sender, EventArgs e)
{
btnRun.Enabled = false;
lblOutputStatus.Text = "";
lblOutputStatus.ForeColor = SystemColors.WindowText;
txtOutput.Clear();
txtOutput.ForeColor = SystemColors.WindowText;
if (String.IsNullOrWhiteSpace(txtSourceFolder.Text) ||
String.IsNullOrWhiteSpace(txtTargetFolder.Text) ||
String.IsNullOrWhiteSpace(txtFileName.Text))
{
txtOutput.AppendText("Error: Source path, target path, and target file name must be specified.");
txtOutput.ForeColor = Color.Red;
}
else
{
try
{
// Create target folder if it doesn't already exist
if (!Directory.Exists(txtTargetFolder.Text))
{
Directory.CreateDirectory(txtTargetFolder.Text);
}
// Build DDF file
DdfFileBuilder ddfBuilder = new DdfFileBuilder(
txtSourceFolder.Text,
chkRecursive.Checked,
txtTargetFolder.Text,
txtFileName.Text,
EnumUtil.SafeParse(cboCompressType.SelectedItem as string, Constants.DefaultCompressionType),
cboCompressMemory.SelectedItem as CompressionWindowSize);
ddfBuilder.BuildAndSave();
// Run "makecab.exe"
string cmd = String.Format("/f {0}", ddfBuilder.DdfFilePath.EnsureQuoted());
Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.CreateNoWindow = true;
startInfo.FileName = "makecab.exe";
startInfo.Arguments = cmd;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.UseShellExecute = false;
process.StartInfo = startInfo;
process.ErrorDataReceived += Process_ErrorDataReceived;
process.OutputDataReceived += Process_OutputDataReceived;
process.Start();
process.BeginOutputReadLine();
process.BeginErrorReadLine();
process.WaitForExit();
txtOutput.AppendText("Exit code: " + process.ExitCode);
if (process.ExitCode == EXIT_CODE_SUCCESS)
{
File.SetLastWriteTime(Path.Combine(txtTargetFolder.Text, txtFileName.Text), DateTime.Now);
lblOutputStatus.Text = "CAB file successfully created.";
lblOutputStatus.ForeColor = Color.Green;
}
else
{
lblOutputStatus.Text = "CAB file not created. See output for details.";
lblOutputStatus.ForeColor = Color.Red;
}
}
catch (Exception ex)
{
lblOutputStatus.Text = "CAB file not created. See output for details.";
lblOutputStatus.ForeColor = Color.Red;
txtOutput.AppendText("Error: " + ex.ToString());
txtOutput.ForeColor = Color.Red;
}
}
btnRun.Enabled = true;
}
// capture output from console stdout to output box on form
private void Process_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate
{
if (e.Data != null)
{
txtOutput.AppendText(e.Data + Environment.NewLine);
}
});
}
// capture output from console stderr to output box on form
private void Process_ErrorDataReceived(object sender, DataReceivedEventArgs e)
{
this.BeginInvoke((MethodInvoker)delegate
{
if (e.Data != null)
{
txtOutput.AppendText(e.Data + Environment.NewLine);
txtOutput.ForeColor = Color.Red;
}
});
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (chkRemember.Checked)
{
UserSettings.Save(
txtSourceFolder.Text,
chkRecursive.Checked,
txtTargetFolder.Text,
txtFileName.Text,
EnumUtil.SafeParse(cboCompressType.SelectedItem as string, Constants.DefaultCompressionType),
cboCompressMemory.SelectedItem as CompressionWindowSize);
}
else
{
UserSettings.Clear();
}
}
private void MainForm_Load(object sender, EventArgs e)
{
// Initialize drop-downs
cboCompressType.Items.AddRange(Enum.GetNames(typeof(CompressionType)));
cboCompressMemory.DataSource = Constants.CompressionWindowSizes;
cboCompressMemory.DisplayMember = "Description";
cboCompressMemory.ValueMember = "Exponent";
// Set field values
UserSettings settings = UserSettings.FetchWithDefaults();
txtSourceFolder.Text = settings.SourcePath;
chkRecursive.Checked = settings.IncludeSubfolders;
txtTargetFolder.Text = settings.TargetPath;
txtFileName.Text = settings.FileName;
cboCompressType.SelectedItem = Enum.GetName(typeof(CompressionType), settings.CompressionType);
cboCompressMemory.SelectedValue = settings.CompressionWindowSize;
lblVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString(2);
}
private void cboCompressType_SelectedIndexChanged(object sender, EventArgs e)
{
CompressionType compressionType = EnumUtil.SafeParse(
cboCompressType.SelectedItem as string, Constants.DefaultCompressionType);
cboCompressMemory.Enabled = DdfFileBuilder.IsCompressionWindowSizeIncluded(compressionType);
}
}
}