C#中通过Process运行Python脚本
2015-12-30 10:47
567 查看
<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"//组合出完整的文件名
相关文章推荐
- leetcode之Binary Tree Paths
- Python 1.5 循环
- leetcode之Sum Root to Leaf Numbers
- 修改ubuntu的默认python版本
- supervisor运行python程序时的环境配置
- python之commands模块
- leetcode之Path Sum
- Python-Windows下安装BeautifulSoup和requests第三方模块
- Python 1.4 条件判断
- BeautifulSoup使用
- 笨方法学Python(6-10)
- python UserBasedCF
- python Item_CF
- python3使用了更多内存优化的技巧,比如,python3的zip就是生成可迭代的对象
- Python Network Programming(7)---泊松分布与发包
- Python 字符串编码中中文字符注意事项
- Python标准库08 多线程与同步 (threading包)
- Python For MySQL 使用连接器连接 ( 一)
- 如何解决Windows下sublime text2不能运行python的问题
- Python学习笔记3-Python元组、数组、字典集合的操作