Python env 从 C# 控制台应用程序激活

Python env activate from C# Console Application

我正在尝试从 C# windows 控制台应用程序/windows 服务激活一个 python 项目环境。但系统无法读取激活文件。如何从 C# 激活我的 python 项目环境?

这是我的代码-

ProcessStartInfo 开始 = new ProcessStartInfo();

        start.FileName = @"\AppData\Local\Programs\Python\Python37\python.exe";
        start.WorkingDirectory = "path to my python project";
        start.Arguments = ".\env\Scripts\activate && python manage.py runserver";// "env/Scripts/activate.bat && python manage.py runserver";// "E:\Blockchain\25.05.2022_Service\backend\env\Scripts\activate";// && E:\Blockchain\07.04.2022 bat file\First Part\Authentication\backend & python manage.py runserver
        start.UseShellExecute = false;           
        start.RedirectStandardOutput = true;          

        using (Process process = Process.Start(start))
        {
            using (StreamReader reader = process.StandardOutput)
            {
                string result = reader.ReadToEnd();
                Console.Write(result);
            }
        }

终于找到解决办法了。这是我从 C#

运行 python django 服务器的完整代码
var process = new Process();

process.StartInfo.FileName = "cmd.exe";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UseShellExecute = false;
// directory of project where env folder is exist.
process.StartInfo.WorkingDirectory = workingDirectory;
process.Start();
/* runServerCommand is for the script to run. 
In my case it was- "env\Scripts\activate && python manage.py runserver" */
process.StandardInput.WriteLine(runServerCommand);
process.StandardInput.Flush();
process.StandardInput.Close();

using (var reader = process.StandardOutput)
{
    var result = reader.ReadToEnd();
    Console.Write(result);
}