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