c# 调用python突然出现问题,我之前用cmd调用python做GNSS,今天准备改改程序,手贱无聊用火绒清理下文件与注册表。然后未动之前成功的代码,运行出现python输出为null,我重新搞了python环境依然没修好(py测试文件能从vs中启动)。如下C#测试代码如下:
using System.Diagnostics;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
namespace test
{
///
/// MainWindow.xaml 的交互逻辑
///
public partial class MainWindow : Window
{
static double [] intArr;
public MainWindow()
{
InitializeComponent();
}
private void CheckBox1_Checked(object sender, RoutedEventArgs e)
{
}
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
}
private static double Azimuth()
{
double Ea = 6378.137; // 赤道半径 Km
double Eb = 6356.725;
var ec = Eb + (Ea - Eb) * (90.0 - 180) / 90.0;
var ed = ec * Math.Cos(DegToRad(148));
var dx = ed * DegToRad(30 - 30);
var dy = ec * DegToRad(180 -0);
double theta = RadToDeg(Math.Atan2(dx, dy));
if (theta < 0)
{
theta = 360 + theta;
}
return theta;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
if (!String.IsNullOrEmpty(txtName.Text))//字符非空
{
if (txtName.Text == "11")
{
string[] strArr = new string[2];//参数列表
string sArguments = @"main.py";//这里是python的文件名字
strArr[0] = "40";
strArr[1] = "116";
RunPythonScript(sArguments, "-u", strArr);
double[] intArrr = intArr;
string c = intArr[0].ToString();
MessageBox.Show(c);
}
}
}
public static double rad(double d)
{
return d * Math.PI / 180.0;
}
private static double DegToRad(double d)
{
return d * Math.PI / 180d;
}
public static double GetAzimuth()
{
double lat1 = rad(148);
double lat2 = rad(150);
double lon1 = rad(32);
double lon2 = rad(30);
double azimuth = Math.Sin(lat1) * Math.Sin(lat2) + Math.Cos(lat1)
* Math.Cos(lat2) * Math.Cos(lon2 - lon1);
azimuth = Math.Sqrt(1 - azimuth * azimuth);
azimuth = Math.Cos(lat2) * Math.Sin(lon2 - lon1) / azimuth;
azimuth = Math.Asin(azimuth) * 180 / Math.PI;
if (Double.IsNaN(azimuth))
{
if (lon1 < lon2)
{
azimuth = 90.0;
}
else
{
azimuth = 270.0;
}
}
return azimuth;
}
public struct PointLonLatAlt
{
public double Lon;
public double Lat;
public double Alt;
public PointLonLatAlt(double lon, double lat, double alt)
{
Lon = lon;
Lat = lat;
Alt = alt;
}
}
//将弧度转化位角度
public static double RadToDeg(double radian)
{
return radian * 180.0 / Math.PI;
}
public static void RunPythonScript(string sArgName, string args = "", params string[] teps)
{
Process p = new Process();
//string path = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase + sArgName;// 获得python文件的绝对路径(将文件放在c#的debug文件夹中可以这样操作)
string path = @"D:\ana\Lib" + sArgName;//(因为我没放debug下,所以直接写的绝对路径,替换掉上面的路径了)
p.StartInfo.FileName = @"D:\ana\python.exe";//(注意:用的话需要换成自己的)没有配环境变量的话,可以像我这样写python.exe的绝对路径(用的话需要换成自己的)。如果配了,直接写"python.exe"即可
string sArguments = path;
foreach (string sigstr in teps)
{
sArguments += " " + sigstr;//传递参数
}
sArguments += " " + args;
p.StartInfo.Arguments = sArguments;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = false;
p.Start();
p.BeginOutputReadLine();
p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
Console.ReadLine();
p.WaitForExit();
}
//输出打印的信息
static void p_OutputDataReceived(object sender, DataReceivedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Data))
{
string b;
Console.WriteLine(e.Data + Environment.NewLine);
string a = e.Data;
b = a;
string[] strArr = a.Split( ' ');
intArr= new double[strArr.Length];
for (int i = 0; i < strArr.Length; i++)
{
intArr[i] = Convert.ToDouble(strArr[i]);
}
a = b;
;
}
}
private void TextBox_TextChanged(object sender, TextChangedEventArgs e)
{
}
private void textname(object sender, TextCompositionEventArgs e)
{
}
}
其中e.date为参数传回C#,一直为null。代码本身在之前测试没有问题,今天 不知为何。我怀疑cmd脚本没有输入python脚本数据。