从C#启动python脚本的方法

时间:2014-03-28 09:27:32

标签: c# python ironpython

我使用了搜索引擎。我找不到我想要的东西,而且我在编程方面也不是那么好。我有一个.py脚本使用 hashlib和M2Crypto ,当我用铁python运行时来自c#的程序,它说没有名为hashlib的模块。我找不到将hashlib导入c#或ironpython的方法,即使我搜索了所有的网,我尝试了下面的代码它似乎也没有用。你能帮忙吗谢谢。

   Process p = new Process(); // create process (i.e., the python program
   GetShortPathName(decdbpath, shortPath, shortPath.Capacity);
   GetShortPathName(db, shortPath2, shortPath2.Capacity);
   p.StartInfo.FileName = "C:\\Python27\\python.exe";
   p.StartInfo.UseShellExecute = false;
   p.StartInfo.Arguments = a+"\\pycode.py" + shortPath + " " + 
                           txt_entermail.Text + " >" + db;
   p.Start(); // start the process (the python program)
   p.WaitForExit();
   MessageBox.Show("Decryption Done");

最后我发现了问题,py脚本的路径包含一个空间我修复了问题,但现在python脚本拒绝接受参数?感谢

string format = string.Format(shortPath + "\\pycode.py"+" "+shortPath2.ToString() + " " + txt_entermail.Text + " > " + shortPath3.ToString());

这是结果:

用法C:\ Users \ win7 \ Ziad \ MOBILE~1 \ DBEXPL~1 \ WINDOW~1 \ bin \ Debug \ pycode.py argument1 argument2>参数3

2 个答案:

答案 0 :(得分:0)

试试并告诉我:

p.Arguments = string.Format("{0} {1}", cmd, args);
     p.UseShellExecute = false;
     p.RedirectStandardOutput = true;
     using(Process process = Process.Start(p))
     {
         using(StreamReader reader = process.StandardOutput)
         {
             string result = reader.ReadToEnd();
             Console.Write(result);
         }
     }

答案 1 :(得分:0)

我相信你的问题出在你的python脚本中,而不是你的c#中。见Unable to import "hashlib"