C#中通过Process运行Python脚本
<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"//组合出完整的文件名
转载自:https://blog.csdn.net/protoss_penguin/article/details/50433046