【问题标题】:Execute embedded python file from c#从c#执行嵌入的python文件
【发布时间】:2019-08-05 16:14:17
【问题描述】:

我的脚本是用 Python 编写的,而 GUI 是用 C# 编写的。这就是为什么我想从 C# 运行 Python(由于各种模块,即 IronPython 不支持,我必须使用原始 Python 版本)。为了保护脚本,我将它嵌入到 .exe 文件中。现在,我怎样才能得到脚本路径? 我之前的代码:

    string python = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + @"\python\python.exe";

    // NOW it obviously does not work
    string scriptPath = @"C:\..\script.py";

    ProcessStartInfo myProcessStartInfo = new ProcessStartInfo(python);
    myProcessStartInfo.UseShellExecute = false;
    myProcessStartInfo.RedirectStandardOutput = true;
    myProcessStartInfo.Arguments = scriptPath;
    Process myProcess = new Process();
    myProcess.StartInfo = myProcessStartInfo;
    myProcess.Start();

Assembly.GetExecutingAssembly().GetManifestResourceStream(...) 在这里不会有用,因为我不想要流(或者我想要吗?),只是路径。

【问题讨论】:

    标签: c# python process path


    【解决方案1】:

    如果脚本是嵌入式资源(How to read embedded resource text file),你可以将其读取为字符串并使用 python.exe 的 -c 参数

    【讨论】:

    • 我会试试的,谢谢。但是如果它是一个相当大的脚本,它不会减慢进程吗?
    猜你喜欢
    • 2021-12-14
    • 2014-11-21
    • 1970-01-01
    • 2014-11-17
    • 2020-10-17
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    相关资源
    最近更新 更多