如何通过将 .cs 文件作为脚本执行来避免编写 .bat 文件?
我想编写一个简单的批处理文件来复制一些文件。
但是,我厌倦了批处理文件,因为它们的限制如此之大,我只想编写一个简单的 .cs 文件,我可以直接从命令行将其作为脚本执行。
如何从命令行运行“.cs”文件并让它运行脚本?
I'd like to write a simple batch file to copy a few files around.
However, I'm tired of batch files as they are so limiting, and I'd like to just write a simple .cs file which I can execute as a script, directly from the command line.
How can I run a ".cs" file from the command line, and have it run an a script?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
除非你编译它,否则你不能。但是,PowerShell具有与C#类似的语法并且非常强大。
You can't unless you compile it. However, PowerShell has a similar syntax to C# and is very powerful.
您需要将其编译成可执行文件,然后运行该可执行文件。
也许你可以编写一个批处理文件来实现这一点?
更严肃地说,请看看 scottm 的答案中提到的 Powershell 作为替代方案(我已相应地 +1 了)。
You would need to compile it into an executable file, and then run this executable.
Maybe you could write a batch file to achieve this?
On a more serious note, do take a look at Powershell as an alternative as mentioned by scottm's answer (which I've +1ed accordingly).
请参阅 http://www.csscript.net/,如果安装它,您可以运行简单的 .cs 文件像DOS批处理文件(即不需要将它们编译成项目)。
See http://www.csscript.net/, if you install it you can run simple .cs files just like DOS batch files (i.e. no need to compile them into a project).
Microsoft Roslyn 允许您从命令行执行 C# 文件,就像批处理文件、PowerShell、Python 等一样。
第 1 步:安装 Roslyn,创建一个新的控制台应用程序,然后使用以下行执行脚本:
或
第 2 步:强> 助理“.csx”文件与编译的控制台应用程序一起执行,它们将像已编译到 Visual Studio 项目中一样执行。
现在我再也不用编写 DOS 批处理文件了。
更新
Microsoft Roslyn allows you to execute a C# file from the command line, just like batch files, PowerShell, Python, etc.
Step 1: Install Roslyn, create a new console app, then use the following line to execute a script:
or
Step 2: Associate ".csx" files with your compiled console app, and they will execute just as if they had been compiled into a visual studio project.
Now I never have to write a DOS batch file in my life, again.
Update
您可以看看如何在命令提示符下编译程序。使用这种技术,您可以输入 .cs 文件,编译 .exe,然后运行 .exe。
http://msdn.microsoft.com/en -us/library/ms172492%28v=vs.80%29.aspx
但是,这似乎有点麻烦。在命令行项目中启动 Visual Studio 并快速输入代码有什么问题吗?
You could take a look at how to compile programs at the command prompt. Using this technique, you could feed in your .cs file, compile the .exe, then run the .exe.
http://msdn.microsoft.com/en-us/library/ms172492%28v=vs.80%29.aspx
However, this seems like it would be a bit cumbersome. What's wrong with just firing up Visual Studio and typing out the code really quick in a command line project?
试试这个。
http://www.sliver.com/dotnet/SnippetCompiler/
不过我不完全确定这就是你想要的。
Try this.
http://www.sliver.com/dotnet/SnippetCompiler/
Tho I'm not entirely sure it's what you're after.