0

So I'm making a WPF application where I'm using CSharpCodeProvider to compile code stored in string. Everything works great except for one part. When I try to use Process.Start(), it gives me "Metadata file 'System.Diagnostics.dll' could not be found" error. I don't know what that means.

using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Dynamically_compile_codes
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {

            CSharpCodeProvider codeProvider = new CSharpCodeProvider(new Dictionary<string, string>() { { "CompilerVersion", txtFrameWork.Text } });
            Button button = (Button)sender;

            CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll","System.Core.dll","System.Diagnostics.dll"}, txtOutput.Text,true);
            //generate exe, not dll
            parameters.GenerateExecutable = true;
            CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, textBox.Text);

            if (results.Errors.HasErrors)
            {
               results.Errors.Cast<CompilerError>().ToList().ForEach(error=> txtStatus.Text+=error.ErrorText+"/r/n");
            }
            else
            {
                //If we clicked run then launch our EXE
                Process.Start(txtOutput.Text);
            }
        }
    }
}

And here is the code that is stored in the string:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;

namespace Hidden_Console_Application
{
    class Program
    {
        static void Main(string[] args)
        {

Process.Start("explorer.exe");
        }
    }
}

1 Answers1

0

System.Diagnostics is a namespace that resides in the System.dll assembly. The CompilerParameters constructor expects a string collection of assembly names and is therefore looking for a loaded assembly named System.Diagnostics which does not exist.

Should be:

CompilerParameters parameters = new CompilerParameters(new[] { "mscorlib.dll","System.Core.dll","System.dll"}, txtOutput.Text,true);
trix
  • 987
  • 8
  • 16