繁体   English   中英

IronPython如何将命令行参数从C#中传递到脚本文件

[英]IronPython how to pass command line arguments to a script file from within C#

IronPython将直接从C#运行而不需要Process.Start

我要加载的脚本文件需要一个参数( filename ),如何用C#将其写出来?

注意:希望这将是Python脚本到Windows GUI应用程序的转换,您可以在其中使用GUI选择文件。

假设一个脚本generator.py看起来像

import sys
fileName = sys.argv[0]
print "working on", fileName

IronPython引擎可以通过以下方式托管在C#/。NET中:

var options = new Dictionary<string, object>();
options["Arguments"] = new[] { @"/somefile.dat" };

var engine = Python.CreateEngine(options);
var runtime = engine.Runtime;
var scope = engine.CreateScope();

var script = engine.CreateScriptSourceFromFile(@"generator.py");
script.Execute(scope);

这导致输出working on /somefile.dat

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM