-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
126 lines (117 loc) · 4.76 KB
/
Program.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
using System;
using System.IO;
using System.Diagnostics;
using Microsoft.Deployment.WindowsInstaller;
namespace signversioned
{
public class MsiProperties
{
public static string Get(string msi, string name)
{
using (Database db = new Database(msi))
{
return db.ExecuteScalar("SELECT `Value` FROM `Property` WHERE `Property` = '{0}'", name) as string;
}
}
}
class Mainclass
{
public static bool ExistsInPath(string fileName)
{
return GetFullPath(fileName) != null;
}
public static string GetFullPath(string fileName)
{
if (File.Exists(fileName))
return Path.GetFullPath(fileName);
var values = Environment.GetEnvironmentVariable("PATH");
foreach (var path in values.Split(Path.PathSeparator))
{
var fullPath = Path.Combine(path, fileName);
if (File.Exists(fullPath))
return fullPath;
}
return null;
}
static void Main(string[] args)
{
if (!ExistsInPath("signtool.exe"))
{
Console.WriteLine(Properties.Resources.ErrorSigntoolExeNotFoundInPath);
return;
}
if (args.Length != 1)
{
Console.WriteLine(Properties.Resources.UsageSignversionedExeTarget);
return;
}
if (!args[0].Equals("params") && !File.Exists(args[0]))
{
Console.WriteLine(Properties.Resources.Error0NotFound, args[0]);
return;
}
if (args[0].Equals("params"))
{
Console.WriteLine(Properties.Resources.PleaseKeyInTheSignerSNameN);
Properties.Settings.Default.SignedBy = Encryption.Encrypt(Console.ReadLine());
Properties.Settings.Default.Save();
Console.WriteLine(Properties.Resources.NPleaseKeyInTheTimeServerN);
Properties.Settings.Default.TimeServer = Encryption.Encrypt(Console.ReadLine());
Properties.Settings.Default.Save();
Console.WriteLine(Properties.Resources.NPleaseKeyInAdditionalParametersEnterForNoneN);
Properties.Settings.Default.AdditionalParams = Encryption.Encrypt(Console.ReadLine());
Properties.Settings.Default.Save();
return;
}
if (string.IsNullOrEmpty(Properties.Settings.Default.SignedBy) || string.IsNullOrEmpty(Properties.Settings.Default.TimeServer))
{
Console.WriteLine(Properties.Resources.ErrorIssuerAndOrTimeServerSettingsNotFound);
return;
}
string path = args[0];
string vPath = path;
string signString = "sign";
signString += " /n \"" + Encryption.Decrypt(Properties.Settings.Default.SignedBy) + "\"";
signString += " /t \"" + Encryption.Decrypt(Properties.Settings.Default.TimeServer) + "\"";
signString += " " + Encryption.Decrypt(Properties.Settings.Default.AdditionalParams);
signString += " " + args[0];
Process signProc = new Process();
ProcessStartInfo signProcStartInfo = new ProcessStartInfo();
signProcStartInfo.FileName = "signtool";
signProcStartInfo.Arguments = signString;
signProcStartInfo.UseShellExecute = false;
signProcStartInfo.RedirectStandardError = true;
signProc.StartInfo = signProcStartInfo;
signProc.Start();
signProc.WaitForExit();
if (signProc.ExitCode == 0)
{
if (vPath.EndsWith(".exe"))
{
string vString = (FileVersionInfo.GetVersionInfo(vPath)).ProductVersion;
vString.Replace(", ", ".");
string vEnding = "-" + vString + ".exe";
vPath = path.Replace(".exe", vEnding);
}
else if (vPath.EndsWith(".msi"))
{
string vEnding = "-" + MsiProperties.Get(path, "ProductVersion") + ".msi";
vPath = path.Replace(".msi", vEnding);
}
try
{
File.Move(path, vPath);
Console.WriteLine(Properties.Resources.Moving0To1Successful, path, vPath);
}
catch
{
Console.WriteLine(Properties.Resources.ErrorMoving0To1Failed, path, vPath);
}
}
else
{
Console.WriteLine(Properties.Resources.ErrorSigning0Failed, path);
}
}
}
}