資源簡介
自己做的關于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#從現象到本質,隨書源碼。
- 下一篇:微博數據復雜網絡用不了,主要轉發關系
評論
共有 條評論