Initial Commit
This commit is contained in:
133
ExeLauncher/Program.cs
Normal file
133
ExeLauncher/Program.cs
Normal file
@@ -0,0 +1,133 @@
|
||||
using Microsoft.CSharp;
|
||||
using System;
|
||||
using System.CodeDom.Compiler;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Windows.Forms;
|
||||
|
||||
namespace ExeLauncher
|
||||
{
|
||||
class Program
|
||||
{
|
||||
const string Paths = "%Paths%";
|
||||
const string Arguments = "%Arguments%";
|
||||
const string WorkingDirectories = "%WorkingDirectories%";
|
||||
|
||||
static string code;
|
||||
|
||||
[STAThread]
|
||||
static void Main(string[] args)
|
||||
{
|
||||
var file = Assembly.GetExecutingAssembly().GetManifestResourceStream("ExeLauncher.Code.cs");
|
||||
var reader = new StreamReader(file);
|
||||
|
||||
code = reader.ReadToEnd();
|
||||
|
||||
reader.Close();
|
||||
|
||||
string path, arguments, workingDirectory, icon;
|
||||
|
||||
Console.Write("Path: ");
|
||||
path = Console.ReadLine();
|
||||
|
||||
Console.Write("Arguments: ");
|
||||
arguments = Console.ReadLine();
|
||||
|
||||
Console.Write("Working Directory: ");
|
||||
workingDirectory = Console.ReadLine();
|
||||
|
||||
Console.WriteLine("Choose an icon for the executable: ");
|
||||
icon = GetIcon();
|
||||
|
||||
Console.WriteLine();
|
||||
|
||||
Console.WriteLine("Creating launcher with options: ");
|
||||
Console.WriteLine($"Path: {path}");
|
||||
Console.WriteLine($"Arguments: {arguments}");
|
||||
Console.WriteLine($"Working directory: {workingDirectory}");
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Choose a location to save the file.");
|
||||
|
||||
var location = GetLocation();
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Generating file...");
|
||||
|
||||
CreateExecutable(location, path, arguments, workingDirectory, icon);
|
||||
|
||||
Console.WriteLine();
|
||||
Console.WriteLine("Done.");
|
||||
Console.ReadKey(true);
|
||||
}
|
||||
|
||||
private static void CreateExecutable(string location, string paths, string arguments, string workingDirectories, string icon)
|
||||
{
|
||||
code = code.Replace(Paths, paths);
|
||||
code = code.Replace(Arguments, arguments);
|
||||
code = code.Replace(WorkingDirectories, workingDirectories);
|
||||
|
||||
using (var codeProvider = new CSharpCodeProvider())
|
||||
{
|
||||
var compilerParameters = new CompilerParameters()
|
||||
{
|
||||
CompilerOptions = $"-target:winexe -win32icon:\"{icon}\"",
|
||||
GenerateExecutable = true,
|
||||
OutputAssembly = location
|
||||
};
|
||||
compilerParameters.ReferencedAssemblies.Add("System.dll");
|
||||
|
||||
var result = codeProvider.CompileAssemblyFromSource(compilerParameters, code);
|
||||
|
||||
if (result.Errors.HasErrors)
|
||||
{
|
||||
Console.WriteLine("An error occured while compiling the executable: ");
|
||||
foreach (var compilerError in result.Errors)
|
||||
{
|
||||
Console.WriteLine("\t{0}", compilerError.ToString());
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private static string GetIcon()
|
||||
{
|
||||
var dialog = new OpenFileDialog()
|
||||
{
|
||||
CheckFileExists = true,
|
||||
Filter = "Icon|*.ico"
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
return dialog.FileName;
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
private static string GetLocation()
|
||||
{
|
||||
var dialog = new SaveFileDialog()
|
||||
{
|
||||
AddExtension = true,
|
||||
DefaultExt = ".exe",
|
||||
FileName = "Run.exe",
|
||||
Filter = "Executable|*.exe",
|
||||
OverwritePrompt = true
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() == DialogResult.OK)
|
||||
{
|
||||
return dialog.FileName;
|
||||
}
|
||||
|
||||
Environment.Exit(1);
|
||||
|
||||
// Impossible to reach, but compiler complained, so...
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user