2014-03-28 117 views
0

我使用了搜索引擎。我找不到我想要的东西,而且我在编程方面也不太好。我有一个使用hashlib和M2Crypto的.py脚本,当我使用铁蟒从c#运行程序时,它说即没有模块命名为hashlib。我找不到将hashlib导入到c#或ironpython的方法,即使我搜索了所有的网络,我也尝试了下面的代码,但它似乎并不工作。请你帮忙,谢谢。从C启动python脚本的方法#

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:\用户\ WIN7 \齐亚德\ MOBILE〜1 \ DBEXPL〜1 \ WINDOW〜1 \ BIN \调试\ pycode.py参数1参数2>参数3

+0

此线程是否对您有帮助? http://stackoverflow.com/questions/11779143/run-a-python-script-from-c-sharp –

+0

我看到它,它没有帮助:( –

+0

我注意到,p.StartInfo.RedirectStandardOutput = true;没有设置为true。你尝试过吗? –

回答

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

根本没有帮助 –

+0

和reader.readtoend();的结果是空的,就像它没有读取脚本idk –

+0

你确定,你的py代码工作正常吗?可能你的错误来自p.Arguments上的连接吗? – JossVAMOS

0

我相信你的问题是在你的Python脚本,而不是在你的C#。见Unable to import "hashlib"

+0

my python脚本工作正常,我试图使用它外部 –

+0

好吧,你可能想确保hashlib是在路径。这是一个答案,说到这一点:https://stackoverflow.com/questions/1371994/importing-external-模块in-ironpython –

+0

另一个直接与hashlib和ironpython有关的问题在这里:https://stackoverflow.com/questions/6855211/can-i-use-all-standard-python-libraries-with-ironpython-or –