简体   繁体   中英

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 !

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. All CS/C# scripts stuffs seem to be 3rd parties.

Do you have any knowledge reference/link for command-line scripts in C# for Windows, pure microsoft style ?

Yes, that would mean dynamic C# compilation... so maybe I am all wrong thinking it is possible. Just tell me.

Option A

If you're looking for a true C# development experience, try CS-Script .

You can use comment directives to use third-party libraries and include other C# scripts.

//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 . C# you write here is real C#. The VS IDE just works.

Option B

Alternatively, take a look at scriptcs

http://scriptcs.net/

You can also use VS to write some simple scripts:

 //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.

C:\\> scriptcs myscript.cs

Also, you can use nuget to install 3rd party packages like NLog

C:\\> scriptcs -install NLog

One problem I found with scriptcs is the inability to use namespaces . scriptcs under the hood uses the Roslyn compiler for compiling C# scripts. Unfortunately, Roslyn has a leaky design implementation when compiling scripts. In particular, in order for Roslyn to handle REPL type of use cases, each script compilation uses nested classes to store variables and state. Since you can't have a namespace in a nested class using namespaces with scriptcs will fail. 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.

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.

You can run scripting through the WSH (Windows Scripting Host):
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 :
http://technet.microsoft.com/en-us/scriptcenter/dd742419

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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