What's a good way to write batch scripts in C#

2019-04-04 18:50发布

I would like to write simple scripts in C#. Stuff I would normally use .bat or 4NT .btm files for. 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. What I'm missing is the easy file-manipulation framework. I want to be able to do cd(".."), copy(srcFile, destFile) type functionality.

Tools I have tried:

  • NANT, which we use in our build process. Not a good scripting tool. Insanely verbose XML syntax and to add a simple function you must write an extension assembly. Can't do it inline.

  • PowerShell. Looks great, but I just haven't been able to switch over to this as my primary shell. Too many differences from 4NT. Whatever I do needs to run from an ordinary command prompt and not require a special shell to run it through. Can PowerShell be used as a script executor?

  • Perl/Python/Ruby. 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.

Are there frameworks out there that are trying to solve this problem of "make a batch file in C#" that you have used?

I want the power of C#/.NET with the immediate-mode type functionality of a typical cmd.exe shell language. Am I alone in wanting something like this?

5条回答
霸刀☆藐视天下
2楼-- · 2019-04-04 19:12

If you have any bash nerds, you can always try cygwin.

Also remember that Python was originally intended as a "glue" langauge. If you used the aforementioned IronPython, it's pretty easy to tie together pre-written C# classes.

查看更多
太酷不给撩
3楼-- · 2019-04-04 19:12

If you are bind to MS, PowerShell is surely the way to go. 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.

查看更多
Evening l夕情丶
4楼-- · 2019-04-04 19:14

I think CS-Script might be the ideal solution for you.

查看更多
Root(大扎)
5楼-- · 2019-04-04 19:21

IronPython and IronRuby do let you leverage all of your .NET "experience and codebase" (they don't answer your objection to learning new languages, however).

查看更多
别忘想泡老子
6楼-- · 2019-04-04 19:22

I would try to get over the PowerShell anxiety because it is the shell of the future. All of the software coming out of Microsoft is using it as their management interface and especially version 2.0 is ridiculously useful.

I'm a C# developer most of the time but PowerShell has solved that whole "WindowsApplication42" problem of temp projects just piling up. 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.

查看更多
登录 后发表回答