C#中简单Winform程序编译(待验证)

1、文件架构
C#中简单Winform程序编译(待验证)_第1张图片
2、MainWindow.xaml

<Window x:Class="WpfApp1.MainWindow"
        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:WpfApp1"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <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>
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Label Content="C# script:" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10 10 0 0"/>
        <TextBox x:Name="txtFilePath" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="120 10 0 0" Width="350" TextWrapping="NoWrap" 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>

3、MainWindow.xaml.cs

namespace WpfApp1
{
    /// 
    /// MainWindow.xaml 的交互逻辑
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            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";

        }
    }
}

4、测试程序

namespace ConsoleApp1
{
    internal class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("This is a Compile Test");
        }
    }
}

你可能感兴趣的:(c#,开发语言)