在C#使用Python腳本文件要注意的的是,首先要將IronPython2.7安裝路徑中的兩個dll文件添加到C#引用中一個是IronPython.dll,另一個是Microsoft.Scripting.dll庫文件;其次在代碼中添加引用如下所示:
using IronPython.Hosting;//引用ironpython文件 using Microsoft.Scripting.Hosting;//引用ironpython文件 //調用方法 public void c_python() { //dd 錯誤嗎定義1 vip視頻地址錯誤 0 正確 String dd, db; ScriptEngine pyEngine = Python.CreateEngine();//創建Python解釋器對象 dynamic py = pyEngine.ExecuteFile(@"play_ve.py");//讀取腳本文件 dynamic pye = pyEngine.ExecuteFile(@"play.py");//讀取腳本文件 try { dd = py.get_url(); dd = dd.ToString(); db =py.get_url2(); db = db.ToString(); Console.WriteLine("dd===" + dd); Console.WriteLine("dd===" + db); } catch (Exception) { Console.WriteLine("123456"); } }
這是python,文件命名play_ve.py
#coding=utf-8 def get_url(): he="1"; return he; def get_url2(): he="5"; return he;