使用WPF开发C#脚本编译器

xaml界面:

<Window x:Class="ZZZTest.Window3"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:ZZZTest"
        mc:Ignorable="d"
        Title="C# Script Runner" Height="450" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Content="C# Script:" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtFilePath" HorizontalAlignment="Left" Margin="120,10,0,0" TextWrapping="NoWrap" VerticalAlignment="Top" Width="350" IsReadOnly="True"/>
        <Button Content="Choose" HorizontalAlignment="Left" Margin="490,10,0,0" VerticalAlignment="Top" Width="75" Click="btnChooseFile_Click"/>
        <Label Content="Output:" Grid.Row="1" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top"/>
        <TextBox x:Name="txtResult" Grid.Row="2" HorizontalAlignment="Stretch" Margin="10,10,10,10" TextWrapping="Wrap" VerticalAlignment="Stretch" Height="200" IsReadOnly="True" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto"/>
        <Button Content="Run" Grid.Row="3" HorizontalAlignment="Left" Margin="10,10,0,10" VerticalAlignment="Bottom" Width="120" Click="btnCompile_Click"/>
        <Button Content="Clear" Grid.Row="3" HorizontalAlignment="Left" Margin="150,10,10,10" VerticalAlignment="Bottom" Width="75" />
    </Grid>
    <Window.Resources>
        <Style TargetType="Label">
            <Setter Property="Foreground" Value="#FF2B2B2B"/>
            <Setter Property="FontSize" Value="14"/>
        </Style>
        <Style TargetType="Button">
            <Setter Property="Background" Value="#FF1BA1E2"/>
            <Setter Property="Foreground" Value="#FFFFFFFF"/>
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="Padding" Value="10"/>
            <Setter Property="Margin" Value="10"/>
            <Setter Property="BorderThickness" Value="0"/>
        </Style>
        <Style TargetType="TextBox">
            <Setter Property="FontSize" Value="14"/>
            <Setter Property="Foreground" Value="#FF2B2B2B"/>
            <Setter Property="Padding" Value="10"/>
            <Setter Property="Margin" Value="10"/>
        </Style>
    </Window.Resources>
</Window>

代码:

using CSScriptLib;
using Microsoft.CodeAnalysis.Scripting;
using Microsoft.CSharp;
using System;
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
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.Shapes;

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

        private void btnChooseFile_Click(object sender, RoutedEventArgs e)
        {
            Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog();
            dlg.Filter = "C# files (*.cs)|*.cs";
            bool? result = dlg.ShowDialog();
            if (result.HasValue && result == true)
            {
                // Open document
                string filename = dlg.FileName;
                txtFilePath.Text = filename;
            }
        }

        private void btnCompile_Click(object sender, RoutedEventArgs e)
        {
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
            parameters.GenerateExecutable = true;
            parameters.OutputAssembly = "output.exe";
            parameters.GenerateInMemory = false;
            parameters.TreatWarningsAsErrors = false;
            //添加需要引用的dll名字
            parameters.ReferencedAssemblies.AddRange(new string[] { "System.dll", "System.Core.dll", "mscorlib.dll" });
            

            string filePath = txtFilePath.Text;
            if (!File.Exists(filePath))
            {
                txtResult.Text = "文件不存在,请选择正确的文件路径!";
                return;
            }

            string source = File.ReadAllText(filePath);
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, source);

            if (results.Errors.HasErrors)
            {
                foreach (CompilerError error in results.Errors)
                {
                    txtResult.Text += $"{error.ErrorText}\n";
                }
                return;
            }

            txtResult.Text += $"编译成功!\n";
            Process.Start("output.exe");
            
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值