【问题标题】:csharpcodeprovider: Can't use Process.Start()csharpcodeprovider:不能使用 Process.Start()
【发布时间】:2019-01-03 22:07:31
【问题描述】:

所以我正在制作一个 WPF 应用程序,我在其中使用 CSharpCodeProvider 来编译存储在字符串中的代码。一切都很好,除了一部分。当我尝试使用 Process.Start() 时,它给了我“找不到元数据文件‘System.Diagnostics.dll’”错误。我不知道那是什么意思。

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);
            }
        }
    }
}

这是存储在字符串中的代码:

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");
        }
    }
}

【问题讨论】:

  • 编译器参数错误,删除“System.Diagnostics.dll”。它是一个命名空间,而不是一个程序集。

标签: c# wpf system.diagnostics process.start csharpcodeprovider


【解决方案1】:

System.Diagnostics 是一个位于 System.dll 程序集中的命名空间。 CompilerParameters 构造函数需要一个程序集名称的字符串集合,因此正在寻找一个名为 System.Diagnostics 的加载程序集,该程序集不存在。

应该是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多