资源简介
自己做的关于c#动态执行代码,读取txt文档中的代码

代码片段和文件信息
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.CodeDom.Compiler;
using System.Reflection;
using Microsoft.CSharp;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button2_Click(object sender EventArgs e)
{
string strLine;
string path;
path = Application.StartupPath + “\\读取代码.txt“;
string st = ““;
FileStream aFile = new FileStream(path FileMode.Open);
StreamReader sr = new StreamReader(aFile);
strLine = sr.ReadLine();
while (strLine != null)
{
st = st + strLine;
st = st + “\n“;
strLine = sr.ReadLine();
}
sr.Close();
string sum = GenerateCode() + “\n“ + st + GenerateCode1();
if (sum != null)
{
// 1.CSharpCodePrivoder
CSharpCodeProvider objCSharpCodePrivoder = new CSharpCodeProvider();
// 2.ICodeComplier
//ICodeCompiler objICodeCompiler = objCSharpCodePrivoder.CreateCompiler();
// 3.CompilerParameters
CompilerParameters objCompilerParameters = new CompilerParameters();
//此处添加引用,使用的代码必须在此处有引用否则会编译错误
objCompilerParameters.ReferencedAssemblies.Add(“System.dll“);
objCompilerParameters.ReferencedAssemblies.Add(“System.Windows.Forms.dll“);
objCompilerParameters.GenerateExecutable = false;
objCompilerParameters.GenerateInMemory = true;
// 4.CompilerResults
CompilerResults cr = objCSharpCodePrivoder.CompileAssemblyFromSource(objCompilerParameters sum);
if (cr.Errors.HasErrors)
{
Console.WriteLine(“编译错误:“);
foreach (CompilerError err in cr.Errors)
{
Console.WriteLine(err.ErrorText);
}
}
else
{
Assembly objAssembly = cr.CompiledAssembly;
object objDynamicAssenmbly = objAssembly.CreateInstance(“Mode.mode“);
MethodInfo objMI = objDynamicAssenmbly.GetType().GetMethod(“OutPut“);
objMI.Invoke(objDynamicAssenmbly null);
}
}
}
public static string GenerateCode()
{
StringBuilder sb = new StringBuilder();
sb.Append(“using System;“);
// sb.Append(Environment.NewLine);
sb.Append(“using System.Windows.Fo
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 10240 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.exe
文件 28160 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.pdb
文件 14328 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe
文件 490 2009-06-11 05:14 读取文档中的代码\WindowsFormsApplication2\bin\Debug\WindowsFormsApplication2.vshost.exe.manifest
文件 34 2011-11-16 10:10 读取文档中的代码\WindowsFormsApplication2\bin\Debug\读取代码.txt
文件 4156 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\Form1.cs
文件 2106 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\Form1.Designer.cs
文件 5814 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\Form1.resx
文件 2230 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.csproj.FileListAbsolute.txt
文件 847 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.csproj.GenerateResource.Cache
文件 10240 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.exe
文件 180 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.Form1.resources
文件 28160 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.pdb
文件 180 2011-11-16 14:35 读取文档中的代码\WindowsFormsApplication2\obj\Debug\WindowsFormsApplication2.Properties.Resources.resources
文件 505 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\Program.cs
文件 1380 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\Properties\AssemblyInfo.cs
文件 2898 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\Properties\Resources.Designer.cs
文件 5612 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\Properties\Resources.resx
文件 1109 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\Properties\Settings.Designer.cs
文件 249 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\Properties\Settings.settings
文件 3759 2011-11-16 14:35 读取文档中的代码\WindowsFormsApplication2\WindowsFormsApplication2.csproj
文件 962 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2.sln
..A..H. 15360 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2.suo
目录 0 2011-11-16 10:04 读取文档中的代码\WindowsFormsApplication2\obj\Debug\Refactor
目录 0 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\obj\Debug\TempPE
目录 0 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\bin\Debug
目录 0 2011-11-19 20:53 读取文档中的代码\WindowsFormsApplication2\obj\Debug
目录 0 2011-11-16 10:43 读取文档中的代码\WindowsFormsApplication2\bin
目录 0 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\obj
目录 0 2011-11-15 21:21 读取文档中的代码\WindowsFormsApplication2\Properties
............此处省略5个文件信息
- 上一篇:C#从现象到本质,随书源码。
- 下一篇:微博数据复杂网络用不了,主要转发关系
相关资源
- C#百度地图源码
- Visual C#.2010从入门到精通配套源程序
- C# 软件版本更新
- C#屏幕软键盘源码,可以自己定制界面
- 智慧城市 智能家居 C# 源代码
- c#获取mobile手机的IMEI和IMSI
- C#实现简单QQ聊天程序
- 操作系统 模拟的 欢迎下载 C#版
- C#写的计算机性能监控程序
- 用C#实现邮件发送,有点类似于outlo
- MVC model层代码生成器 C#
- c#小型图书销售系统
- C# Socket Server Client 通讯应用 完整的服
- c# winform 自动登录 百度账户 源代码
- C#编写的16进制计算器
- C#TCP通信协议
- C# 数据表(Dataset)操作 合并 查询一
- C#语音识别系统speechsdk51,SpeechSDK51L
- 数据库备份还原工具1.0 C# 源码
-
[免费]xm
lDocument 节点遍历C# - EQ2008LEDc#开发实例
- DirectX.Capturec# winform 操作摄像头录像附
- c# 实现的最大最小距离方法对鸢尾花
- C#版保龄球记分代码
- C#自定义控件
- 基于c#的实验室设备管理系统621530
- C# 使用ListView控件实现图片浏览器(源
- C#简单窗体聊天程序
- C#指纹识别系统程序 报告
- c# 高校档案信息管理系统
评论
共有 条评论