C#中通過Process執行Python指令碼
阿新 • • 發佈:2019-02-14
<pre name="code" class="csharp">
<pre name="code" class="csharp"><span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="csharp">public void set_Python_Argv() { IFeatureLayer pFeatureLayer = (IFeatureLayer)m_pLayer; IDataLayer dl = (IDataLayer)m_pLayer; IWorkspaceName ws = ((IDatasetName)(dl.DataSourceName)).WorkspaceName; string path = ws.PathName; //設定指令碼引數 string sArguments = @"shape_analyze.py";<span style="white-space:pre"> </span>//這裡是python的檔名字 string file_name = m_pLayer.Name;<span style="white-space:pre"> </span>//要被傳入的檔名,不含字尾 RunPythonScript(sArguments, path, file_name); } public static void RunPythonScript(string sArgName, string ws_name, string file_name) {//呼叫指令碼 string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + sArgName;// 獲得python檔案的絕對路徑 string sArguments = path; if (ws_name.Length > 0 || file_name.Length > 0) { sArguments =sArguments + " " + ws_name + " " + file_name;//傳遞引數 } //設定程序並執行 Process p = new Process(); p.StartInfo.FileName = "F:\\ArcGIS10.2\\python.exe"; p.StartInfo.Arguments = sArguments; p.Start(); p.WaitForExit(); }
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">以上程式碼是C#+Arc Engine專案的一部分,傳入一個.shp(圖層檔案)到shape_analyze.py,以命令列引數的形式傳遞</span>
ws_name = str(sys.argv[1])<span style="white-space:pre"> </span>//接受引數 file_name = str(sys.argv[2]) fc = ws_name + "\\" + file_name + ".shp"//組合出完整的檔名