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

news2024/9/28 11:08:36

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
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    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");
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/789657.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

vue中的异步请求Axios(个人学习笔记五)

目录 友情提醒第一章、传统的jQuery方式获取数据1.1&#xff09;后端controller层代码1.2&#xff09;传统的jQuery获取数据1.3&#xff09;使用vue对象和jQuery获取异步数据 第二章、使用Axios获取数据2.1&#xff09;axios简介2.2&#xff09;axios两种使用方式2.3&#xff0…

WEB:easyphp

背景知识 php弱类型比较 MD5碰撞 题目 进行代码审计 <?php highlight_file(__FILE__); $key1 0;//值赋值 $key2 0;$a $_GET[a];//get方法获取值 $b $_GET[b];if(isset($a) && intval($a) > 6000000 && strlen($a) < 3){ //a的值需要大于 60000…

Seaborn中怎样绘制双变量分布图?

两个变量的二元分布可视化也很有用。在 Seaborn中最简单的方法是使用 jointplot()函数&#xff0c;该函数可以创建一个多面板图形&#xff0c;比如散点图、二维直方图、核密度估计等&#xff0c;以显示两个变量之间的双变量关系及每个变量在单坐标轴上的单变量分布。 jointplo…

Linux--Block group

Block Group&#xff1a;ext2文件系统会根据分区的大小划分为数个Block Group。而每个Block Group都有着相 同的结构组成。政府管理各区的例子 超级块&#xff08;Super Block&#xff09;&#xff1a;存放文件系统本身的结构信息。记录的信息主要有&#xff1a;bolck 和 inod…

App测试流程及测试点

1 APP测试基本流程 1.1流程图 1.2测试周期 测试周期可按项目的开发周期来确定测试时间&#xff0c;一般测试时间为两三周&#xff08;即15个工作日&#xff09;&#xff0c;根据项目情况以及版本质量可适当缩短或延长测试时间。正式测试前先向主管确认项目排期。 1.3测试资源…

测试覆盖率 JVM 字节码测试运用 - 远程调试、测试覆盖、影子数据库

目录 前言&#xff1a; 简介 基础使用方式介绍 工具特性 前言&#xff1a; 在软件开发中&#xff0c;测试覆盖率是一个非常重要的指标&#xff0c;它表示代码中所有的测试用例是否都已经被覆盖到。JVM 字节码测试是一种比较新的测试方法&#xff0c;它可以对 JVM 字节码进…

【雕爷学编程】Arduino动手做(86)---4*4位 WS2812 全彩模块4

37款传感器与执行器的提法&#xff0c;在网络上广泛流传&#xff0c;其实Arduino能够兼容的传感器模块肯定是不止这37种的。鉴于本人手头积累了一些传感器和执行器模块&#xff0c;依照实践出真知&#xff08;一定要动手做&#xff09;的理念&#xff0c;以学习和交流为目的&am…

【Postman】Postman接口测试进阶用法详解:断言、全局与环境变量、关联、批量执行用例、读取外部文件实现参数化

文章目录 一、Postman断言1、断言位置2、Postman的常用断言3、操作实例 二、全局变量与环境变量1、二者区分2、设置全局变量3、设置环境变量 三、Postman接口关联1、概念2、操作步骤 四、批量执行测试用例1、操作步骤2、查看结果 五、读取外部文件实现参数化1、使用场景2、操作…

云服务器远程nacos服务注册失败/不健康Client not connected, current status:STARTING

文章目录 Nacos报错docker安装不用 docker安装 Nacos报错 docker安装 使用docker在云服务器安装Nacos之后出现Client not connected, current status:STARTING 使用docker 安装之后需要添加映射端口 docker run -e JAVA_OPTS"-Xms256m -Xmx256m"-e MODEstandalone…

7.24 作业

1.自己封装vector template<typename T> class Myverctor {T* first;T* last;T* end; public:Myverctor():first(NULL),last(NULL),end(NULL){}Myverctor(int num,T data):first(new T[num]){last end first num;for(int i 0;i<num;i) first[i] data;}Myverctor…

【ROS2 Foxy】Rviz2 不支持可视化压缩图像消息

这里我通过订阅话题&#xff0c;压缩图像消息是存在的&#xff1a; ros2 topic echo /hk_camera/rgb/compressed从官方代码库的 issue 中了解到&#xff0c;在 Foxy 版本的 Rviz2 是不支持压缩图像消息的可视化的&#xff0c;现在 Foxy 也已经停止维护了&#xff0c;以后更不太…

redis 1

shell 1&#xff1a;安装1. 源码安装&#xff08;CENTOS&#xff09; 2.999:可能会出现得问题1. 编译出错 1&#xff1a;安装 1. 源码安装&#xff08;CENTOS&#xff09; 官方下载源码包 wget https://download.redis.io/redis-stable.tar.gz # 安装依赖 yum install gcc解压…

node 版本管理器 nvm

文章目录 一、卸载node二、nvm 下载三、nvm 安装四、检测环境变量是否一致五、nvm常见问题 一、卸载node 打开cmd命令行窗口&#xff0c;输入npm cache clean --force 回车执行 打开控制面板&#xff0c;在控制面板中把Node.js卸载 二、nvm 下载 nvm全英文也叫node.js ve…

7.25 Qt

制作一个登陆界面 login.pro文件 QT core guigreaterThan(QT_MAJOR_VERSION, 4): QT widgetsCONFIG c11# The following define makes your compiler emit warnings if you use # any Qt feature that has been marked deprecated (the exact warnings # depend on …

cocosCreator 之 ScrollView

版本&#xff1a;3.4.0 参考&#xff1a;ScrollView组件 简介 ScrollView组件作为滚动容器来使用&#xff0c;它的实现通过ScrollBar组件来展示内容的位置和Mask组件显示指定区域&#xff0c;来保证有限的区域内显示更多的内容。 它的构成部分&#xff1a; ScrollBar滚动条相…

3、Winform表单控件

在学习了布局控件之后,我们就该学习表单控件了。 程序的本质=输入+处理+输出。在Winform程序角度,这里的输入输出就可以用我们的表单控件来实现。 表单控件大致可分为两类,选项控件和文本控件。 文本控件 文本控件常用的有两种,分别是TextBox和RichTextBox TextBox T…

01 矩阵(力扣)多源广度优先搜索 JAVA

给定一个由 0 和 1 组成的矩阵 mat &#xff0c;请输出一个大小相同的矩阵&#xff0c;其中每一个格子是 mat 中对应位置元素到最近的 0 的距离。 两个相邻元素间的距离为 1 。 输入&#xff1a;mat [[0,0,0],[0,1,0],[0,0,0]] 输出&#xff1a;[[0,0,0],[0,1,0],[0,0,0]] 输入…

修改小说阅读器

感谢这个作者的插件&#xff1a;https://ext.dcloud.net.cn/plugin?id2485 1. 搬入后page TypeError: Cannot read property page of undefined 该问题已经解决&#xff0c;有两种方法。第一种&#xff1a;直接注释掉 &#xff0c;第二种 修改为vue3的方式 // 取消ios左滑返…

数据结构和算法一(空间复杂度、时间复杂度等算法入门)

时间复杂度&#xff1a; 空间复杂度&#xff1a; 时间比空间重要 递归&#xff1a; 递归特征&#xff1a; 递归案例&#xff1a; 汉诺塔问题&#xff1a; def hanoi(n,A,B,C):if n>0:hanoi(n-1,A,C,B)print("moving from %s to %s"%(A,C))hanoi(n-1,B,A,C)hanoi…

java中的动态代理机制

目录 什么是动态代理&#xff1f; 为什么需要代理&#xff1f; 代理长什么样子&#xff1f; 代码样例 什么是动态代理&#xff1f; 动态代理可以无侵入式的给代码增加功能 为什么需要代理&#xff1f; 对象如果嫌弃身上的事情太多&#xff0c;就可以通过代理来转移部分的…