-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainForm.cs
121 lines (105 loc) · 4.48 KB
/
MainForm.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
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.IO;
using System.Windows.Forms;
namespace ExWrapper
{
public partial class MainForm : Form
{
public MainForm()
{
InitializeComponent();
}
private bool Source2Exe(string source, string icon, bool showConsole, string outputExe)
{
CSharpCodeProvider compiler = new CSharpCodeProvider();
CompilerParameters comParms = new CompilerParameters();
comParms.GenerateExecutable = true;
comParms.GenerateInMemory = false;
comParms.IncludeDebugInformation = false;
comParms.MainClass = "ExWrapper.Wrapper.Program";
//如果加/target:winexe可以生成gui exe
comParms.CompilerOptions = "/optimize";
if (!showConsole)
comParms.CompilerOptions += " /target:winexe";
comParms.OutputAssembly = outputExe;
comParms.TreatWarningsAsErrors = false;
comParms.ReferencedAssemblies.AddRange(new string[] { "mscorlib.dll", "System.dll",
"System.Data.dll", "System.Xml.dll" });
if (!String.IsNullOrEmpty(icon))
{
comParms.CompilerOptions += $" /win32icon:\"{icon}\"";
}
CompilerResults comRes = compiler.CompileAssemblyFromSource(comParms, source);
return comRes.Errors != null && comRes.Errors.Count == 0;
}
private string escape2src(string old)
{
return old.Replace("\"", "\"\"");
}
private void button1_Click(object sender, EventArgs e)
{
if (radioEmbed.Checked && string.IsNullOrWhiteSpace(textEmbed.Text) ||
radioCall.Checked && string.IsNullOrWhiteSpace(textRun.Text))
{
MessageBox.Show("You must set a valid run content", "ExWrapper");
}
else
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Title = "Save As";
saveFileDialog.Filter = "Exe Files (*.exe)|*.exe";
saveFileDialog.OverwritePrompt = true;
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string outputExe = saveFileDialog.FileName;
string source = Properties.Resources.Wrapper;
if (radioEmbed.Checked)
{
source = source.Replace("${embed}", escape2src(textEmbed.Text));
}
else
{
source = source.Replace("${cmd}", escape2src(textRun.Text));
source = source.Replace("${para}", escape2src(textPara.Text));
source = source.Replace("${dir}", escape2src(textDir.Text));
}
source = source.Replace("${hide}", cbShowConsole.Checked ? "false" : "true");
Source2Exe(source, textIcon.Text, cbShowConsole.Checked, outputExe);
}
}
}
}
private void radioEmbed_CheckedChanged(object sender, EventArgs e)
{
groupCall.Enabled = radioCall.Checked;
groupEmbed.Enabled = radioEmbed.Checked;
}
private void btnOpen_Click(object sender, EventArgs e)
{
using (OpenFileDialog openDialog = new OpenFileDialog())
{
openDialog.Title = "Select a executable file";
openDialog.Filter = "Executable Files (*.*)|*.*";
if (openDialog.ShowDialog() == DialogResult.OK)
{
textRun.Text = Path.GetFileName(openDialog.FileName);
}
}
}
private void btnSelectIcon_Click(object sender, EventArgs e)
{
using (OpenFileDialog openDialog = new OpenFileDialog())
{
openDialog.Title = "Select a icon file";
openDialog.Filter = "Icon Files (*.ico)|*.ico";
if (openDialog.ShowDialog() == DialogResult.OK)
{
textIcon.Text = openDialog.FileName;
}
}
}
}
}