Initial Commit

This commit is contained in:
2021-12-04 11:30:41 +01:00
commit 14fb199206
8 changed files with 928 additions and 0 deletions

133
ExeLauncher/Program.cs Normal file
View 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;
}
}
}