C# 从C启动python脚本的方法#

C# 从C启动python脚本的方法#,c#,python,ironpython,C#,Python,Ironpython,我用了搜索引擎。我找不到我想要的东西,而且我编程也不是很好。我有一个.py脚本,它使用了hashlib和M2Crypto,当我使用iron python从c运行程序时,它说没有名为hashlib的模块。我找不到将hashlib导入c#或ironpython的方法,即使我搜索了整个网络,我也尝试了以下代码,但似乎不起作用。你能帮忙吗?谢谢 Process p = new Process(); // create process (i.e., the python program Get

我用了搜索引擎。我找不到我想要的东西,而且我编程也不是很好。我有一个.py脚本,它使用了hashlib和M2Crypto,当我使用iron 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>argument3试试看,告诉我:

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);
         }
     }

我相信你的问题在于你的python脚本,而不是你的c。请参见

此线程是否有用?我看到了,它没有帮助:(我注意到p.StartInfo.RedirectStandardOutput=true;没有设置为true。你试过了吗?是的,我试过了,但没有成功,结果来自阅读器。readtoend();为空,就像它没有读取脚本IDKAR一样。你确定,你的py代码工作正常吗?你的错误可能来自p参数的串联。是的,当我从windows启动standalone时,它正常工作。你在参数上使用带空格的路径吗?GetShortPathName()-为您提供了我导入的kernel32.dll方法的短路径,因此我对路径中的白色间隔没有问题。我的python脚本工作正常,我尝试在外部使用它,您可能希望确保hashlib在路径中。以下是一个答案:另一个与hashlib和ironpython直接相关的问题: