2013/11/13

C#.Net 透過C#調用Python Iron Python

聽過Iron Python,卻沒有嘗試過!
有篇文章『深究 CLR : IronPython』是在探討Iron Python,有興趣就看看吧!

首先到Iron Python網站下載,並安裝好!


將Microsoft.Scripting.dll以及IronPython.dll加入參照




範例程式碼用『Python 讀取檔案內容』內的


using System;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;

namespace CSharp_Call_Python
{
    class Program
    {
        static void Main(string[] args)
        {
            ScriptEngine engine = Python.CreateEngine();

            engine.ExecuteFile(@"You:\are\python\file\path.py");

            Console.ReadKey();
        }
    }
}


執行看看,結果『我愛你』變成亂碼





參考資料:
http://stackoverflow.com/questions/11779143/run-a-python-script-from-c-sharp
http://www.voidspace.org.uk/ironpython/ip_in_ip.shtml
http://stackoverflow.com/questions/10123296/reading-utf-8-file-with-codecs-in-ironpython