从文件执行C#代码 [英] Execute C# code from file

查看:98
本文介绍了从文件执行C#代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须在按钮点击事件中执行一些c#代码。

我在记事本中写了一些代码。我能够从记事本中读取所有代码并存储到字符串中。但之后我不知道如何执行这些字符串作为c#代码



我尝试过:



I have to execute some c# piece of code at button click event.
I have written some piece of code in notepad. I am able to read all code from notepad and storing into string. But after that I don't know how to execute those string as c# code

What I have tried:

string[] lines = System.IO.File.ReadAllLines(@"F:\PragyaNew\Demo.txt");
            foreach (string line in lines)
            {
            CSharpCodeProvider provider = new CSharpCodeProvider();
            CompilerParameters parameters = new CompilerParameters();
            // Reference to System.Drawing library
            parameters.ReferencedAssemblies.Add("System.Drawing.dll");
            // True - memory generation, false - external file generation
            parameters.GenerateInMemory = true;
            // True - exe file generation, false - dll file generation
            parameters.GenerateExecutable = true;
            CompilerResults results = provider.CompileAssemblyFromSource(parameters, line);
            }

推荐答案

使用此谷歌搜索:编译并运行c - Google搜索 [ ^ ]



我发现:

* 在运行时编译C#代码 [ ^ ];和

* 编译器构造 - 是否可以动态编译和执行C#代码片段? - 堆栈溢出 [ ^ ]
Using this google search: compile and run c - Google Search[^]

I found:
* Compiling C# Code at Runtime[^]; and
* compiler construction - Is it possible to dynamically compile and execute C# code fragments? - Stack Overflow[^]

path = Server.MapPath(/ folderName / fileName.txt); //如果你的txt文件在项目本身中

string [] lines;

var list = new List< string>();

var fileStream = new FileStream(@ path,FileMode.Open,FileAccess.Read);

using(var streamReader = new StreamReader(fileStream,Encoding.UTF8))

{

string line;

while((line = streamReader.ReadLine()) != null)

{

list.Add(line);

}

}

lines = list.ToArray();



StringBuilder builder = new StringBuilder();

foreach(行中的字符串值) )

{

builder.Append(value);

builder.Append(
);

}

div_Content.InnerHtml = builder.ToString();



可能有助于读取c#div_Content中的txt文件是我的div id 路径是你的路径位置
path = Server.MapPath("/folderName/fileName.txt");//if your txt file is in project itself
string[] lines;
var list = new List<string>();
var fileStream = new FileStream(@path, FileMode.Open, FileAccess.Read);
using (var streamReader = new StreamReader(fileStream, Encoding.UTF8))
{
string line;
while ((line = streamReader.ReadLine()) != null)
{
list.Add(line);
}
}
lines = list.ToArray();

StringBuilder builder = new StringBuilder();
foreach (string value in lines)
{
builder.Append(value);
builder.Append("
");
}
div_Content.InnerHtml = builder.ToString();

may it will help to read the txt file in c# div_Content is my div id path is your path location


我的记事本代码正在执行。我已经解决了。但是我面临的另一个问题是,当我在文本框中输入一些值并提交表单时,我正在尝试加载文件并执行这些文件。文件正在执行,但我无法通过文件代码获取文本框值。
My notepad code is executing. I have solved. But again one problem I am facing that when I enter some value in textbox and submit form then I am trying to load file and executing those file. File is executing but I am not able to get textbox value thorough file code.


这篇关于从文件执行C#代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆