16
21

More than 5 years have passed since last update.

バッチファイルにC#コードを書く

Last updated at Posted at 2017-07-17

注意

実用性は不明です。

方法

バッチファイル内にPowerShellスクリプトを書くことができます。
 (参考 : バッチファイルから PowerShell を呼び出す方法 )

PowerShellスクリプト内にC#プログラムを書くことができます。
 (参考 : PowerShellメモ C#コードを実行 )

組み合わせると、
バッチファイル内にC#コードを書くことができます。

サンプル

以下をバッチファイル "hello.bat" に保存して、"hello.bat 1" を実行すると、"hello 1 world" が2行表示されます。

@powershell -NoProfile -ExecutionPolicy Unrestricted "$s=[scriptblock]::create((gc \"%~f0\"|?{$_.readcount -gt 1})-join\"`n\");&$s" %*&goto:eof

# http://qiita.com/cd01/items/82829ba0ec0f59e1b04d
# http://qiita.com/Kosen-amai/items/d68b9590c9e8e8b06377

Param($argBat)

$src = @'
using System;
public static class HelloClass
{
    public static string HelloMethod(string arg)
    {
        string msg = "hello " + arg + " world";
        Console.WriteLine(msg);
        return msg;
    }
}
'@
Add-Type -TypeDefinition $src -Language CSharp

$helloResult = [HelloClass]::HelloMethod($argBat)
Write-Output -InputObject $helloResult

exit 0

関連情報

16
21
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
16
21