I have a Windows Forms Application with a CheckBox and a button. The button will make it so the CodeDOM compiled exe will disable task manager. But if they don't want it to disable, they can uncheck the button. What I'm trying to to is make it so if they uncheck the button it will remove the block of code from the resource file that disables task manager.
This is my resource file:
using System;
using System.Windows.Forms;
using Microsoft.Win32;
static class Program
{
[STAThread]
static void Main()
{
RegistryKey regkey;
string keyValueInt = "1";
string subKey = "Software\\Microsoft\\Windows\\CurrentVersion\\Policies\\System";
try
{
regkey = Registry.CurrentUser.CreateSubKey(subKey);
regkey.SetValue("DisableTaskMgr", keyValueInt);
regkey.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
My main code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.CodeDom.Compiler;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string source = Properties.Resources.BaseSource;
if (checkBox1.Checked == false)
{
// Not sure what to put here
}
CompilerResults results = null;
using (SaveFileDialog sfd = new SaveFileDialog() { Filter = "Executable|*.exe"})
{
if (sfd.ShowDialog() == DialogResult.OK)
{
results = Build(source, sfd.FileName);
}
}
if (results.Errors.Count > 0)
foreach (CompilerError error in results.Errors)
Console.WriteLine("{0} at line {1}",
error.ErrorText, error.Line);
else
Console.WriteLine("Succesfully compiled!");
Console.ReadLine();
}
private static CompilerResults Build(string source, string fileName)
{
CompilerParameters parameters = new CompilerParameters()
{
GenerateExecutable = true,
ReferencedAssemblies = { "System.dll", "System.Windows.Forms.dll" },
OutputAssembly = fileName,
TreatWarningsAsErrors = false
};
CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
return provider.CompileAssemblyFromSource(parameters, source);
}
}
}