第一次在这里问一个问题,希望它符合礼仪。 有谁知道如何使用C#
process.UseShellExecute = false;
在 Unity3D 时?使用它设置为false会导致exe崩溃。但是将其设置为true会禁用发送writeline命令的功能。这些建议和解决方案都受到欢迎。感谢大家!代码示例如下:
ProcessStartInfo processinfo = new
ProcessStartInfo("C:/ConsoleExample/bin/Debug/ConsoleExample.exe");
processinfo.RedirectStandardOutput = false;
processinfo.RedirectStandardError = false;
processinfo.RedirectStandardInput = true;
processinfo.UseShellExecute = false; //<----Causes program to crash exe when launched, but is required for write console key.
processinfo.CreateNoWindow = false;
Process.Start(processinfo);
Thread.Sleep(1000);
//Write text to console exe file from unity as a command.
processinfo.StandardInput.Write(ConsoleKey.Escape);
答案 0 :(得分:2)
所以我最终得到了这个。感谢@Hristo和@Programmer的帮助和评论。非常感激。这是激活控制台应用程序并发送密钥的最终代码。如果有人有疑问或需要更多详细信息,请通过邮寄或留言告诉我,我会在此发布更多信息。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Diagnostics;
using System.Threading;
using UnityEngine.UI;
using UnityStandardAssets.CrossPlatformInput;
public class Camera_controller : MonoBehaviour {
public int caminterrupt;
private IEnumerator coroutine;
// Use this for initialization
void Start () {
caminterrupt = 0;
coroutine = startcamera();
}
// Update is called once per frame
void Update () {
}
public void button1(string pressed)
{
if (caminterrupt == 0)
{
caminterrupt = 1;
StartCoroutine(coroutine);
}
else
{ caminterrupt = 0;
StopCoroutine("coroutine");
}
}
public IEnumerator startcamera() {
//string strOutput;
//Starting Information for process like its path, use system shell i.e.
control process by system etc.
ProcessStartInfo psi = new
ProcessStartInfo(@"C:\ConsoleExample\bin\Debug\ConsoleExample.exe");
// its states that system shell will not be used to control the process
instead program will handle the process
psi.UseShellExecute = false;
psi.ErrorDialog = false;
// Do not show command prompt window separately
//psi.CreateNoWindow = true;
//psi.WindowStyle = ProcessWindowStyle.Hidden;
//redirect all standard inout to program
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
//create the process with above infor and start it
Process plinkProcess = new Process();
plinkProcess.StartInfo = psi;
plinkProcess.Start();
//link the streams to standard inout of process
StreamWriter myStream = new
StreamWriter(plinkProcess.StandardInput.BaseStream, Encoding.ASCII);
//send command to cmd prompt and wait for command to execute with thread
sleep
refire:
if (caminterrupt == 1)
{
myStream.WriteLine("y");
yield return new WaitForSeconds(1);
goto refire;
}
if (caminterrupt == 0)
{
myStream.WriteLine("e");
}
myStream.WriteLine("e");
// flush the input stream before sending exit command to end process for
any unwanted characters
myStream.Close();
plinkProcess.Close();
caminterrupt = 0;
}
}
这是Unity端调用控制台文件并通过发送键命令进行交互。