如何通过将 .cs 文件作为脚本执行来避免编写 .bat 文件?

发布于 2025-01-03 21:39:11 字数 131 浏览 2 评论 0原文

我想编写一个简单的批处理文件来复制一些文件。

但是,我厌倦了批处理文件,因为它们的限制如此之大,我只想编写一个简单的 .cs 文件,我可以直接从命令行将其作为脚本执行。

如何从命令行运行“.cs”文件并让它运行脚本?

I'd like to write a simple batch file to copy a few files around.

However, I'm tired of batch files as they are so limiting, and I'd like to just write a simple .cs file which I can execute as a script, directly from the command line.

How can I run a ".cs" file from the command line, and have it run an a script?

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

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

发布评论

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

评论(7

演出会有结束 2025-01-10 21:39:11

除非你编译它,否则你不能。但是,PowerShell具有与C#类似的语法并且非常强大。

You can't unless you compile it. However, PowerShell has a similar syntax to C# and is very powerful.

一桥轻雨一伞开 2025-01-10 21:39:11

您需要将其编译成可执行文件,然后运行该可执行文件。

也许你可以编写一个批处理文件来实现这一点?


更严肃地说,请看看 scottm 的答案中提到的 Powershell 作为替代方案(我已相应地 +1 了)。

You would need to compile it into an executable file, and then run this executable.

Maybe you could write a batch file to achieve this?


On a more serious note, do take a look at Powershell as an alternative as mentioned by scottm's answer (which I've +1ed accordingly).

╭⌒浅淡时光〆 2025-01-10 21:39:11

请参阅 http://www.csscript.net/,如果安装它,您可以运行简单的 .cs 文件像DOS批处理文件(即不需要将它们编译成项目)。

See http://www.csscript.net/, if you install it you can run simple .cs files just like DOS batch files (i.e. no need to compile them into a project).

友谊不毕业 2025-01-10 21:39:11

Microsoft Roslyn 允许您从命令行执行 C# 文件,就像批处理文件、PowerShell、Python 等一样。

第 1 步:安装 Roslyn,创建一个新的控制台应用程序,然后使用以下行执行脚本:

scriptEngine.ExecuteFile("DemoClass.cs");

scriptEngine.ExecuteFile("DemoScript.csx");

第 2 步:强> 助理“.csx”文件与编译的控制台应用程序一起执行,它们将像已编译到 Visual Studio 项目中一样执行。

现在我再也不用编写 DOS 批处理文件了。

更新

Microsoft Roslyn allows you to execute a C# file from the command line, just like batch files, PowerShell, Python, etc.

Step 1: Install Roslyn, create a new console app, then use the following line to execute a script:

scriptEngine.ExecuteFile("DemoClass.cs");

or

scriptEngine.ExecuteFile("DemoScript.csx");

Step 2: Associate ".csx" files with your compiled console app, and they will execute just as if they had been compiled into a visual studio project.

Now I never have to write a DOS batch file in my life, again.

Update

我三岁 2025-01-10 21:39:11

您可以看看如何在命令提示符下编译程序。使用这种技术,您可以输入 .cs 文件,编译 .exe,然后运行 ​​.exe。

http://msdn.microsoft.com/en -us/library/ms172492%28v=vs.80%29.aspx

但是,这似乎有点麻烦。在命令行项目中启动 Visual Studio 并快速输入代码有什么问题吗?

You could take a look at how to compile programs at the command prompt. Using this technique, you could feed in your .cs file, compile the .exe, then run the .exe.

http://msdn.microsoft.com/en-us/library/ms172492%28v=vs.80%29.aspx

However, this seems like it would be a bit cumbersome. What's wrong with just firing up Visual Studio and typing out the code really quick in a command line project?

×眷恋的温暖 2025-01-10 21:39:11

试试这个。
http://www.sliver.com/dotnet/SnippetCompiler/
不过我不完全确定这就是你想要的。

Try this.
http://www.sliver.com/dotnet/SnippetCompiler/
Tho I'm not entirely sure it's what you're after.

万劫不复 2025-01-10 21:39:11
    $Source = (gc csharpfilename.cs|Out-String)
    #compile Csharp
    Add-Type -TypeDefinition $Source -Language CSharp

    #Call you function like this
    [namespaceName.className]::functionName("Parameters/Arguments")
    $Source = (gc csharpfilename.cs|Out-String)
    #compile Csharp
    Add-Type -TypeDefinition $Source -Language CSharp

    #Call you function like this
    [namespaceName.className]::functionName("Parameters/Arguments")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文