C# 中的 Windows shell 脚本

发布于 2024-11-17 13:36:56 字数 333 浏览 4 评论 0原文

我很确定几个月前我读到了有关启动 Windows 脚本的可能性,不仅是通常的 .bat,还包括 .js.vbs和<代码>.cs!

今天早上我愿意尝试 C# .cs 文件中的 Windows 脚本,但我在网上找不到任何参考资料。所有 CS/C# 脚本似乎都是第三方的。

您是否有任何关于 C# for Windows 命令行脚本的知识参考/链接,纯微软风格?

是的,这意味着动态 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

迷爱 2024-11-24 13:36:56

选项 A

如果您正在寻找真正的 C# 开发体验,请尝试 CS-Script

您可以使用注释指令来使用第三方库并包含其他 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");
        }
    }
}

就我个人而言,我更喜欢 CS-Script,因为我注意到 1) 与脚本相比更快脚本启动时间下面的 scriptcs2) 使用命名空间没有问题。你这里写的C#才是真正的C#。 VS IDE 就可以工作了。

选项 B

或者,查看 scriptcs

http://scriptcs.net/

您还可以使用 VS 编写一些简单的脚本:

 //MyScript.Main()

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

当您准备好使用 scriptcs 运行脚本时,删除 // 注释。

<代码>C:\> scriptcs myscript.cs

另外,您可以使用 nuget 安装第 3 方软件包,例如 NLog

C:\> scriptcs -install NLog

我发现 scriptcs 的一个问题是无法使用命名空间scriptcs 在底层使用 Roslyn 编译器来编译 C# 脚本。不幸的是,Roslyn 在编译脚本时存在漏洞设计实现。特别是,为了让 Roslyn 处理 REPL 类型的用例,每个脚本编译都使用嵌套类来存储变量和状态。由于使用namespaces和scriptcs在嵌套class中不能拥有命名空间,因此将会失败。因此,如果您尝试导入包含命名空间的松散 *.cs 文件(例如 VS 项目中的域对象),则需要删除命名空间。

这个糟糕的设计决策给我留下了不好的印象,因此我主要使用 cs-script 选项 A,直到 Roslyn 团队支持命名空间。

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.

盗心人 2024-11-24 13:36:56

您可以通过 WSH(Windows 脚本主机)运行脚本:
http://msdn.microsoft.com/en-我们/library/9bbdkx3k(v=vs.85).aspx

You can run scripting through the WSH (Windows Scripting Host):
http://msdn.microsoft.com/en-us/library/9bbdkx3k(v=vs.85).aspx

忘羡 2024-11-24 13:36:56

另一个为 yoyu 提供强大功能和您可能感兴趣的可用命令的解决方案是 PowerShell
http://technet.microsoft.com/en-us/scriptcenter/dd742419

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文