-
Notifications
You must be signed in to change notification settings - Fork 27
/
WSAppBak.cs
296 lines (277 loc) · 8.55 KB
/
WSAppBak.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
using System;
using System.Diagnostics;
using System.IO;
using System.Xml;
namespace WSAppBak
{
internal class WSAppBak
{
private string AppName = "Windows Store App Backup";
private string AppCreator = "Kiran Murmu";
private string AppCurrentDirctory = Directory.GetCurrentDirectory();
private string WSAppXmlFile = "AppxManifest.xml";
private bool Checking = true;
private string WSAppName;
private string WSAppPath;
private string WSAppVersion;
private string WSAppFileName;
private string WSAppOutputPath;
private string WSAppProcessorArchitecture;
private string WSAppPublisher;
public void Run()
{
ReadArg();
}
private string RunProcess(string fileName, string args)
{
string result = "";
Process process = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = fileName,
Arguments = args,
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
}
};
process.Start();
while (!process.StandardOutput.EndOfStream)
{
string text = process.StandardOutput.ReadLine();
Console.WriteLine(text);
if (text.Length > 0)
{
result = text;
}
}
return result;
}
private void ReadArg()
{
while (Checking)
{
Console.Clear();
Console.WriteLine("\t\t'{0}' by {1}", AppName, AppCreator);
Console.WriteLine("================================================================================");
Console.Write("Enter the App path: ");
WSAppPath = Convert.ToString(Console.ReadLine());
if (WSAppPath.Contains("\""))
{
WSAppPath = WSAppPath.Replace("\"", "");
WSAppPath = "\"" + WSAppPath + "\"";
}
else if (File.Exists(WSAppPath + "\\" + WSAppXmlFile))
{
while (Checking)
{
Console.Write("\nEnter the Output path: ");
WSAppOutputPath = Convert.ToString(Console.ReadLine());
if (WSAppOutputPath.Contains("\""))
{
WSAppOutputPath = WSAppOutputPath.Replace("\"", "");
WSAppOutputPath = "\"" + WSAppOutputPath + "\"";
}
else if (Directory.Exists(WSAppOutputPath))
{
WSAppFileName = Path.GetFileName(WSAppPath);
using (XmlReader xmlReader = XmlReader.Create(WSAppPath + "\\" + WSAppXmlFile))
{
while (xmlReader.Read())
{
if (xmlReader.IsStartElement() && xmlReader.Name == "Identity")
{
string text = xmlReader["Name"];
if (text != null)
{
WSAppName = text;
}
string text2 = xmlReader["Publisher"];
if (text2 != null)
{
WSAppPublisher = text2;
}
string text3 = xmlReader["Version"];
if (text3 != null)
{
WSAppVersion = text3;
}
string text4 = xmlReader["ProcessorArchitecture"];
if (text4 != null)
{
WSAppProcessorArchitecture = text4;
}
}
}
}
while (Checking)
{
MakeAppx();
}
}
else
{
Checking = true;
Console.WriteLine("\nInvailed Output Path, '{0}' Directory not found!", WSAppOutputPath);
Console.Write("Press any Key to retry...");
Console.ReadKey();
Console.Clear();
Console.WriteLine("\t\t'{0}' by {1}", AppName, AppCreator);
Console.Write("================================================================================");
}
}
}
else
{
Checking = true;
Console.WriteLine("\nInvailed App Path, '{0}' file not found!", WSAppXmlFile);
Console.Write("Press any Key to retry...");
Console.ReadKey();
}
}
}
private void MakeAppx()
{
string text = AppCurrentDirctory + "\\WSAppBak\\MakeAppx.exe";
string args = "pack -d \"" + WSAppPath + "\" -p \"" + WSAppOutputPath + "\\" + WSAppFileName + ".appx\" -l";
if (File.Exists(text))
{
if (File.Exists(WSAppOutputPath + "\\" + WSAppFileName + ".appx"))
{
File.Delete(WSAppOutputPath + "\\" + WSAppFileName + ".appx");
}
Console.WriteLine("\nPlease wait.. Creating '.appx' package file.\n");
if (RunProcess(text, args).ToLower().Contains("succeeded"))
{
Console.Clear();
Console.WriteLine("\t\t'{0}' by {1}", AppName, AppCreator);
Console.WriteLine("================================================================================");
Console.WriteLine("Package '{0}' creation succeeded.", WSAppFileName + ".appx");
while (Checking)
{
MakeCert();
}
}
else
{
Checking = false;
Console.Clear();
Console.WriteLine("\t\t'{0}' by {1}", AppName, AppCreator);
Console.WriteLine("================================================================================");
Console.Write("Package '{0}' creation failed... press any Key to exit.", WSAppFileName + ".appx");
Console.ReadKey();
}
}
else
{
Checking = false;
Console.WriteLine("\nCan't create '.appx' file, 'MakeAppx.exe' file not found!");
Console.Write("Press any Key to exit...");
Console.ReadKey();
}
}
private void MakeCert()
{
string text = AppCurrentDirctory + "\\WSAppBak\\MakeCert.exe";
string args = "-n \"" + WSAppPublisher + "\" -r -a sha256 -len 2048 -cy end -h 0 -eku 1.3.6.1.5.5.7.3.3 -b 01/01/2000 -sv \"" + WSAppOutputPath + "\\" + WSAppFileName + ".pvk\" \"" + WSAppOutputPath + "\\" + WSAppFileName + ".cer\"";
if (File.Exists(text))
{
if (File.Exists(WSAppOutputPath + "\\" + WSAppFileName + ".pvk"))
{
File.Delete(WSAppOutputPath + "\\" + WSAppFileName + ".pvk");
}
if (File.Exists(WSAppOutputPath + "\\" + WSAppFileName + ".cer"))
{
File.Delete(WSAppOutputPath + "\\" + WSAppFileName + ".cer");
}
Console.WriteLine("\nPlease wait.. Creating certificate for the package.\n");
Console.Write("Certificate creation: ");
if (RunProcess(text, args).ToLower().Contains("succeeded"))
{
while (Checking)
{
Pvk2Pfx();
}
}
else
{
Checking = false;
Console.WriteLine("\nFailed to create Certificate for the package... Prees any Key exit.");
Console.ReadKey();
}
}
else
{
Checking = false;
Console.WriteLine("\nCan't create Certificate for the package, 'MakeCert.exe' file not found!");
Console.Write("Press any Key to exit...");
Console.ReadKey();
}
}
private void Pvk2Pfx()
{
string text = AppCurrentDirctory + "\\WSAppBak\\Pvk2Pfx.exe";
string args = "-pvk \"" + WSAppOutputPath + "\\" + WSAppFileName + ".pvk\" -spc \"" + WSAppOutputPath + "\\" + WSAppFileName + ".cer\" -pfx \"" + WSAppOutputPath + "\\" + WSAppFileName + ".pfx\"";
if (File.Exists(text))
{
if (File.Exists(WSAppOutputPath + "\\" + WSAppFileName + ".pfx"))
{
File.Delete(WSAppOutputPath + "\\" + WSAppFileName + ".pfx");
}
Console.WriteLine("\nPlease wait.. Converting certificate to sign the package.\n");
Console.Write("Certificate convertion: ");
if (RunProcess(text, args).Length == 0)
{
Console.Write("succeeded");
while (Checking)
{
SignApp();
}
}
else
{
Checking = false;
Console.WriteLine("\nCan't convert certificate to sign the package... Prees any Key exit...");
Console.ReadKey();
}
}
else
{
Checking = false;
Console.WriteLine("\nCan't convert Certificate to sign the package, 'Pvk2Pfx.exe' file not found!");
Console.Write("Press any Key to exit...");
Console.ReadKey();
}
}
private void SignApp()
{
string text = AppCurrentDirctory + "\\WSAppBak\\SignTool.exe";
string args = "sign -fd SHA256 -a -f \"" + WSAppOutputPath + "\\" + WSAppFileName + ".pfx\" \"" + WSAppOutputPath + "\\" + WSAppFileName + ".appx\"";
if (File.Exists(text))
{
Console.WriteLine("\n\nPlease wait.. Signing the package, this may take some minutes.\n");
if (RunProcess(text, args).ToLower().Contains("successfully signed"))
{
Checking = false;
Console.WriteLine("Package signing succeeded. Please install the '.cer' file to [Local Computer\\Trusted Root Certification Authorities] before install the App Package or use 'WSAppPkgIns.exe' file to install the App Package!");
Console.Write("\nPress any Key to exit..... :)");
Console.ReadKey();
}
else
{
Checking = false;
Console.WriteLine("\nCan't Sign the package, Press any Key to exit...");
Console.ReadKey();
}
}
else
{
Checking = false;
Console.WriteLine("\nCan't Sign the package, 'SignTool.exe' file not found!");
Console.Write("Press any Key to exit...");
Console.ReadKey();
}
}
}
}