简体   繁体   English

在C#中编写批处理脚本的好方法是什么?

[英]What's a good way to write batch scripts in C#?

I would like to write simple scripts in C#. 我想用C#编写简单的脚本。 Stuff I would normally use .bat or 4NT .btm files for. 我通常会使用.bat或4NT .btm文件。 Copying files, parsing text, asking user input, and so on. 复制文件,解析文本,询问用户输入等。 Fairly simple but doing this stuff right in a batch file is really hard (no exceptions for example). 相当简单,但在批处理文件中执行此操作非常困难(例如,没有例外)。

I'm familiar with command line "scripting" wrappers like AxScript so that gets me part of the way there. 我熟悉像AxScript这样的命令行“脚本”包装器,这样我就可以使用它了。 What I'm missing is the easy file-manipulation framework. 我缺少的是简单的文件操作框架。 I want to be able to do cd(".."), copy(srcFile, destFile) type functionality. 我希望能够做cd(“..”),copy(srcFile,destFile)类型的功能。

Tools I have tried: 我试过的工具:

  • NANT, which we use in our build process. NANT,我们在构建过程中使用它。 Not a good scripting tool. 不是一个好的脚本工具。 Insanely verbose XML syntax and to add a simple function you must write an extension assembly. 疯狂冗长的XML语法并添加一个简单的函数,您必须编写扩展程序集。 Can't do it inline. 不能内联。

  • PowerShell. 电源外壳。 Looks great, but I just haven't been able to switch over to this as my primary shell. 看起来很棒,但我只是无法切换到我的主要shell。 Too many differences from 4NT. 与4NT有太多不同。 Whatever I do needs to run from an ordinary command prompt and not require a special shell to run it through. 无论我做什么都需要从普通的命令提示符运行而不需要特殊的shell来运行它。 Can PowerShell be used as a script executor? PowerShell可以用作脚本执行程序吗?

  • Perl/Python/Ruby. 的Perl / Python的/红宝石。 Really hate learning an entirely new language and framework just to do batch file operations. 真的讨厌学习一种全新的语言和框架来进行批处理文件操作。 Haven't been able to dedicate the time I need to do this. 无法投入我需要的时间来做这件事。 Plus, we're a 99% .NET shop for our toolchain and I really want to leverage our existing experience and codebase. 另外,我们的工具链是99%的.NET商店,我真的想利用我们现有的经验和代码库。

Are there frameworks out there that are trying to solve this problem of "make a batch file in C#" that you have used? 是否有框架试图解决您使用的“在C#中创建批处理文件”的问题?

I want the power of C#/.NET with the immediate-mode type functionality of a typical cmd.exe shell language. 我希望C#/ .NET的强大功能具有典型cmd.exe shell语言的立即模式类型功能。 Am I alone in wanting something like this? 我一个人想要这样的东西吗?

I would try to get over the PowerShell anxiety because it is the shell of the future. 我会试着克服PowerShell的焦虑,因为它是未来的外壳。 All of the software coming out of Microsoft is using it as their management interface and especially version 2.0 is ridiculously useful. 所有来自Microsoft的软件都将它用作管理界面,尤其是2.0版本非常有用。

I'm a C# developer most of the time but PowerShell has solved that whole "WindowsApplication42" problem of temp projects just piling up. 我大部分时间都是C#开发人员,但PowerShell解决了临时项目的整个“WindowsApplication42”问题。 PowerShell gives you full access to the .NET framework in a command line shell so even if you don't know how to do something in PowerShell, you most likely know how to do it in .NET. PowerShell使您可以在命令行shell中完全访问.NET框架,因此即使您不知道如何在PowerShell中执行某些操作,您也很可能知道如何在.NET中执行此操作。

IronPython和IronRuby 确实可以让您利用所有的.NET“经验和代码库”(但他们不会回答您对学习新语言的反对意见)。

If you have any bash nerds, you can always try cygwin . 如果你有任何bash书呆子,你可以尝试cygwin

Also remember that Python was originally intended as a "glue" langauge. 还记得Python最初是作为“胶水”语言。 If you used the aforementioned IronPython , it's pretty easy to tie together pre-written C# classes. 如果您使用前面提到的IronPython ,那么将预先编写的C#类绑定在一起非常容易。

我认为CS-Script可能是您理想的解决方案。

If you are bind to MS, PowerShell is surely the way to go. 如果你绑定到MS,PowerShell肯定是要走的路。 But I don't like it much. 但我不喜欢它。

I personally use MSBuild script more, and would like to see Mono C# Shell one day comes to Windows. 我个人更多地使用MSBuild脚本,并希望看到Mono C#Shell有一天会来到Windows。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用受保护对象C#编写单元测试的好方法是什么(使用NMock和NUnit框架) - What's a good way to write Unit tests for code with protected objects C# (using NMock, and NUnit framework) 在C#中捕获StackOverflow异常的一般方法是什么? - What's a good general way of catching a StackOverflow exception in C#? 在C#中,什么是显示可缩放,可平移视频的好方法? - In C#, what's a good way for displaying zoomable, pannable video? 在C#项目中存储不变变量的好方法是什么? - What's a good way to store unchanging variables in a C# project? 编写C#TSR的可接受方式是什么? - What's the accepted way to write a C# TSR? 在C#中将HTML写入WebBrowser的正确方法是什么? - What's the proper way to write HTML to a WebBrowser in C#? 在C#中编写[0..100]的最佳方法是什么? - What's the best way to write [0..100] in C#? 在C#中将short []数组写入文件的最佳方法是什么? - What's the best way to write a short[] array to a file in C#? 什么是C#中的错误操作符? - What's the false operator in C# good for? 来自C背景,在C#中实现const引用数据表/结构的好方法是什么? - Coming from a C background, what's a good way to implement const reference data tables/structures in C#?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM