嗨,根据我的上一个问题here我尝试编写一个sql编辑器或类似的东西,这样我尝试从C#连接到CMD并执行我的命令。 现在我的问题是我连接到SQLPLUS后,我无法获得SQLPLUS命令,我审查的其他资源不满足我。请帮助我如何在连接到sqlplus之后,我可以在我的进程中运行我的sql命令吗?现在我使用这段代码:
//Create process
System.Diagnostics.Process pProcess = new System.Diagnostics.Process();
//strCommand is path and file name of command to run
pProcess.StartInfo.FileName = strCommand;
//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = strCommandParameters;
pProcess.StartInfo.UseShellExecute = false;
//Set output of program to be written to process output stream
pProcess.StartInfo.RedirectStandardOutput = true;
//Optional
pProcess.StartInfo.WorkingDirectory = strWorkingDirectory;
//Start the process
pProcess.Start();
//Get program output
string strOutput = pProcess.StandardOutput.ReadToEnd();
//Wait for process to finish
pProcess.WaitForExit();
我定制了它。我将初始化分开,我创建过程对象一次我还有问题,运行第二个命令我使用这些代码进行第二次调用:
pProcess.StartInfo.FileName = strCommand;
//strCommandParameters are parameters to pass to program
pProcess.StartInfo.Arguments = strCommandParameters;
//Start the process
pProcess.Start();
//Get program output
string strOutput = pProcess.StandardOutput.ReadToEnd();
//Wait for process to finish
pProcess.WaitForExit();
提前致谢
答案 0 :(得分:6)
你的问题有点令人困惑,但我想我看到了你的问题。首先,您应该查看此博客文章,查看common issues with System.Diagnostics.Process。您的代码碰巧违反了那里未列出的代码。重用Process对象本身。
您需要重构代码,如:
class MyProcessStarter
{
private ProcessStartInfo _startInfo = new ProcessStartInfo();
public MyProcessStarter(string exe, string workingDir)
{
_startInfo.WorkingDirectory = workingDir;
_startInfo.FileName = exe;
_startInfo.UseShellExecute = false;
_startInfo.RedirectStandardOutput = true;
}
public string Run(string arguments)
{
_startInfo.Arguments = arguments;
Process p = Process.Start(_startInfo);
p.Start();
string strOutput = p.StandardOutput.ReadToEnd();
p.WaitForExit();
return strOutput;
}
}
我写了一个更完整,更准确的实现,名为ProcessRunner。以下演示了执行相同操作的用法:
using CSharpTest.Net.Processes;
partial class Program
{
static int Main(string[] args)
{
ProcessRunner run = new ProcessRunner("svn.exe");
run.OutputReceived += new ProcessOutputEventHandler(run_OutputReceived);
return run.Run("update", "C:\\MyProject");
}
static void run_OutputReceived(object sender, ProcessOutputEventArgs args)
{
Console.WriteLine("{0}: {1}", args.Error ? "Error" : "Output", args.Data);
}
}
答案 1 :(得分:1)
在发送另一个命令之前,您需要从输入中读取所有数据!
如果没有数据可用,你就不能要求阅读...有点不好意思?
我的解决方案...当要求阅读时...要求阅读一个大缓冲区...就像1 MEGA ......
你需要等待100分钟......样本代码......
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim oProcess As New Process()
Dim oStartInfo As New ProcessStartInfo("cmd.exe", "")
oStartInfo.UseShellExecute = False
oStartInfo.RedirectStandardOutput = True
oStartInfo.RedirectStandardInput = True
oStartInfo.CreateNoWindow = True
oProcess.StartInfo = oStartInfo
oProcess.Start()
oProcess.StandardInput.WriteLine("dir")
Threading.Thread.Sleep(100)
Dim Response As String = String.Empty
Dim BuffSize As Integer = 1024 * 1024
Dim bytesRead As Integer = 0
Do
Dim x As Char() = New Char(BuffSize - 1) {}
bytesRead = oProcess.StandardOutput.Read(x, 0, BuffSize)
Response = String.Concat(Response, String.Join("", x).Substring(0, bytesRead))
Loop While oProcess.StandardOutput.Peek >= 0
MsgBox(Response)
Response = String.Empty
oProcess.StandardInput.WriteLine("dir c:\")
Threading.Thread.Sleep(100)
bytesRead = 0
Do
Dim x As Char() = New Char(BuffSize - 1) {}
bytesRead = oProcess.StandardOutput.Read(x, 0, BuffSize)
Response = String.Concat(Response, String.Join("", x).Substring(0, bytesRead))
'Response = String.Concat(Response, String.Join("", x))
Loop While oProcess.StandardOutput.Peek >= 0
MsgBox(Response)
End Sub
End Class