简体   繁体   English

C#中的Windows shell脚本

[英]Windows shell scripts in C#

I was pretty sure that some months ago I read about the possibility to launch Windows scripts not only the usual .bat , but also .js , .vbs and .cs ! 我很确定几个月前我读到有关启动Windows脚本的可能性,不仅是通常的.bat ,还有.js.vbs.cs

I was willing to try windows scripts in C# .cs files this morning, but I can't happen to find any references on the web. 我愿意今天早上在C# .cs文件中尝试使用Windows脚本,但我无法在网上找到任何引用。 All CS/C# scripts stuffs seem to be 3rd parties. 所有CS / C#脚本的东西似乎都是第三方。

Do you have any knowledge reference/link for command-line scripts in C# for Windows, pure microsoft style ? 你有任何关于C#for Windows命令行脚本的知识参考/链接,纯微软风格?

Yes, that would mean dynamic C# compilation... so maybe I am all wrong thinking it is possible. 是的,这意味着动态C#编译......所以也许我认为这是可能的。 Just tell me. 就告诉我嘛。

Option A 选项A.

If you're looking for a true C# development experience, try CS-Script . 如果您正在寻找真正的C#开发体验,请尝试使用CS-Script

You can use comment directives to use third-party libraries and include other C# scripts. 您可以使用comment指令来使用第三方库并包含其他C#脚本。

//css_nuget dotnetzip
using Ionic.Zip;
using System;

class Script
{
    static public void Main()
    {
        using(ZipFile zip = new ZipFile())
        {
            zip.AddFile("app.exe");
            zip.AddFile("readme.exe");
            zip.Save("app.zip");
        }
    }
}

Personally, I prefer CS-Script because I've noticed 1) faster script start-up times compared to scriptcs below and 2) no issues using namespaces . 就个人而言,我更喜欢CS-Script相比,因为我注意到1) 更快的脚本启动时间scriptcs以下; 2)没有问题,使用的命名空间 C# you write here is real C#. 你在这里写的C#是真正的C#。 The VS IDE just works. VS IDE正常工作。

Option B 选项B.

Alternatively, take a look at scriptcs 或者,看一下scriptcs

http://scriptcs.net/ http://scriptcs.net/

You can also use VS to write some simple scripts: 您还可以使用VS编写一些简单的脚本:

 //MyScript.Main()

 public class MyScript{
     public static void Main(){
          Console.WriteLine("hello world.");
     }
 }

When you're ready to run scripts with scriptcs , remove the // comment. 当您准备好使用scriptcs运行脚本时,请删除//注释。

C:\\> scriptcs myscript.cs

Also, you can use nuget to install 3rd party packages like NLog 此外,您可以使用nuget安装第三方软件包,如NLog

C:\\> scriptcs -install NLog

One problem I found with scriptcs is the inability to use namespaces . 我在scriptcs发现的一个问题是无法使用命名空间 scriptcs under the hood uses the Roslyn compiler for compiling C# scripts. scriptcs下的脚本使用Roslyn编译器来编译C#脚本。 Unfortunately, Roslyn has a leaky design implementation when compiling scripts. 不幸的是,Roslyn在编译脚本时有一个漏洞的设计实现。 In particular, in order for Roslyn to handle REPL type of use cases, each script compilation uses nested classes to store variables and state. 特别是,为了让Roslyn处理REPL类型的用例,每个脚本编译都使用嵌套类来存储变量和状态。 Since you can't have a namespace in a nested class using namespaces with scriptcs will fail. 由于使用带有scriptcs的命名空间在嵌套class使用命名空间scriptcs将失败。 So if you try to import loose *.cs files that contain namespaces (like domain objects from a VS project), you'll need to strip out the namespaces. 因此,如果您尝试导入包含名称空间的松散*.cs文件(如VS项目中的域对象),则需要删除名称空间。

This awful design decision left a bad impression on me, so I've mostly used cs-script Option A until the Roslyn team supports namespaces. 这个糟糕的设计决定给我留下了不好的印象,所以我主要使用cs-script 选项A,直到Roslyn团队支持命名空间。

You can run scripting through the WSH (Windows Scripting Host): 您可以通过WSH(Windows Scripting Host)运行脚本:
http://msdn.microsoft.com/en-us/library/9bbdkx3k(v=vs.85).aspx http://msdn.microsoft.com/en-us/library/9bbdkx3k(v=vs.85).aspx

Another solution which gives yoyu a lot of power and available commands you might find interesting is PowerShell : 另一个为yoyu提供大量功能和可用命令的解决方案是PowerShell
http://technet.microsoft.com/en-us/scriptcenter/dd742419 http://technet.microsoft.com/en-us/scriptcenter/dd742419

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM