如何从C#程序中的文件运行命令 [英] How to run commands from a file inside a C# program

查看:82
本文介绍了如何从C#程序中的文件运行命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以使C#从文本文件运行命令.例如:我已经构建了一个程序,它会打开文本文件,如果其中包含C#代码,它将运行它.但是我知道它不能运行每个可用的命令,否则我将不得不引用每个系统组件,在我的情况下这将是乏味且不必要的.但是,是否可以通过文本文件在程序内部运行代码?

I would like to know if there was a way I could get C# to run commands from a text file. For example: I have built a program and it opens text files and if there is C# code inside it it will run it. But I know that it couldn''t run every command available or else I''d have to make reference to every system component which would be tedious and unecessary in my case. But is there a way of running code inside a program from a text file?

推荐答案

您正在寻找CSharpCodeProvider.查看此链接

http://support.microsoft.com/kb/304655 [ http://stackoverflow.com/questions/4063285/missing-assembly-references-在动态编译的代码中 [ ^ ]

您必须使用CompilerParameters.ReferencedAssemblies.Add(...)添加引用,但我认为没有简单的方法可以解决此问题.如果要单独添加所需的程序集,则可能必须自己构建C#解析器.

当然这不是不可能的,您可以使用YACC/Bison(解析器)和Lex(词法分析器)来完成,然后将它们链接到您的应用程序,但是显然这并不容易!但这绝对是有趣的读物!
You are looking for CSharpCodeProvider. Check out this link

http://support.microsoft.com/kb/304655[^]

http://stackoverflow.com/questions/4063285/missing-assembly-references-in-dynamically-compiled-code[^]

You HAVE to add the references using CompilerParameters.ReferencedAssemblies.Add(...) and I don''t think there is a simple way to work around this. If you want to add required assemblies alone, you may have to build a C# parser yourself.

Of course that''s not impossible and you could do it using YACC/Bison (parsers) and Lex (lexical analyzers) and then link them to your application, but obviously it isn''t easy! But they are definitely an interesting read!


首先,我知道它不是文本文件,但可以帮助您从文件中运行C#代码.

您可能想要做的是创建一个.bat文件.然后执行此文件.

创建一个bat文件以运行C#代码 [ ^ ]

要运行bat文件,请使用以下代码;

First of all, I know it is not a text file but that might help you running C# codes from a file.

What you might want to do is create a .bat file. And then execute this file.

Creta a bat file to run C# Code[^]

For running the bat file here is the code;

System.Diagnostics.Process proc = new System.Diagnostics.Process(); // Declare New Process
                proc.StartInfo.FileName = fileName;
                proc.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
                proc.StartInfo.CreateNoWindow = true;
                proc.Start();
                proc.WaitForExit();



基本上,这可以做到;



Basically this will do it tho;

System.Diagnostics.Process.Start(@"C:\listfiles.bat");


这完全取决于您想要的命令内容.

如果这是C#代码,它也很容易解决,但是涉及很多编程.
您可以在此页面上找到我过去的所有答案和参考资料中的所有详细信息:
使用CodeDom生成代码 [
It all depends on what you want to have for commands.

If this is C# code, it is quite solvable, too, but involves a lot of programming.
You can find all the details in my past Answers and references on this page:
code generating using CodeDom[^].

—SA


这篇关于如何从C#程序中的文件运行命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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