forked from sapientcoder/CabMaker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Form1.cs
234 lines (192 loc) · 8.19 KB
/
Form1.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Windows.Forms;
namespace CabMaker
{
public partial class Form1 : Form
{
private const int MAX_LINES_IN_DDF = 1024;
private const int EXIT_CODE_SUCCESS = 0;
public Form1()
{
InitializeComponent();
}
private void btnSourceBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
txtSourceFolder.Text = dialog.SelectedPath;
}
}
private void btnTargetBrowse_Click(object sender, EventArgs e)
{
FolderBrowserDialog dialog = new FolderBrowserDialog();
dialog.SelectedPath = txtSourceFolder.Text;
if (dialog.ShowDialog() == DialogResult.OK)
{
txtTargetFolder.Text = dialog.SelectedPath;
}
}
private List<DdfFileRow> GetFiles(string sDir)
{
List<DdfFileRow> list = new List<DdfFileRow>();
string srcPath = txtSourceFolder.Text;
foreach (string f in Directory.GetFiles(sDir))
{
list.Add(new DdfFileRow()
{
FullName = f,
Path = f.Replace(srcPath, "").TrimStart('\\')
});
}
if (chkRecursive.Checked)
{
foreach (string d in Directory.GetDirectories(sDir))
{
list.AddRange(GetFiles(d));
}
}
return list;
}
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);
}
string ddfPath = Path.Combine(txtTargetFolder.Text, txtFileName.Text + ".ddf");
// Build DDF file
StringBuilder ddf = new StringBuilder();
ddf.AppendFormat(@";*** MakeCAB Directive file;
.OPTION EXPLICIT
.Set CabinetNameTemplate={0}
.Set DiskDirectory1={1}
.Set MaxDiskSize=0
.Set Cabinet=on
.Set Compress=on
", txtFileName.Text.EnsureQuoted(), txtTargetFolder.Text.EnsureQuoted());
int ddfHeaderLines = ddf.ToString().Split(new[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Length;
int maxFiles = MAX_LINES_IN_DDF - ddfHeaderLines; // only write enough files to hit the max # of lines allowed in a DDF (blank lines don't count)
List<DdfFileRow> ddfFiles = GetFiles(txtSourceFolder.Text);
foreach (var ddfFile in ddfFiles.Take(maxFiles))
{
ddf.AppendFormat("\"{0}\" \"{1}\"{2}", ddfFile.FullName, ddfFile.Path, Environment.NewLine);
}
File.WriteAllText(ddfPath, ddf.ToString(), Encoding.Default);
string cmd = String.Format("/f {0}", ddfPath.EnsureQuoted());
// Run "makecab.exe"
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 Form1_FormClosing(object sender, FormClosingEventArgs e)
{
IsolatedStorageFile storage = IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
bool save = chkRemember.Checked;
UserSettings settings = new UserSettings()
{
SourcePath = (save ? txtSourceFolder.Text : ""),
TargetPath = (save ? txtTargetFolder.Text : ""),
FileName = (save ? txtFileName.Text : ""),
IncludeSubfolders = (save ? chkRecursive.Checked : true)
};
storage.SaveObject(settings, $"{Application.ProductName}.dat");
}
private void Form1_Load(object sender, EventArgs e)
{
IsolatedStorageFile storage = IsolatedStorageFile.GetStore(
IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null);
UserSettings settings = storage.LoadObject<UserSettings>($"{Application.ProductName}.dat");
if (settings != null)
{
txtSourceFolder.Text = settings.SourcePath;
txtTargetFolder.Text = settings.TargetPath;
txtFileName.Text = settings.FileName;
}
lblVersion.Text = Assembly.GetExecutingAssembly().GetName().Version.ToString(2);
}
}
}