using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
namespace Java编译器
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string txtFileName = "";//存放文件名;
private string txtfilename = "";//存放文件路径;
private void button1_Click(object sender, EventArgs e)
{
if (txtfilename != "")
{
richTextBox1.SaveFile(txtfilename,
RichTextBoxStreamType.PlainText);
}
else
{
SaveFileDialog savefile = new SaveFileDialog();
savefile.Filter = "Java文件|*.java";
if (savefile.ShowDialog() == DialogResult.OK)
{
richTextBox1.SaveFile(savefile.FileName,
RichTextBoxStreamType.PlainText);
}
try
{
FileInfo fileInfo = new FileInfo(savefile.FileName);
txtfilename = savefile.FileName;
txtFileName = fileInfo.Name;
}
catch
{
return;
}
}
}
private void button2_Click(object sender, EventArgs e)
{
string str1 = "cd /d " + txtfilename.Replace(@"\" + txtFileName, "");
string str2 = "javac " + txtFileName;
string str = cmd(str1, str2);
richTextBox2.Text = str;
}
public string cmd(string str1, string str2)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";//要执行的程序名称
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;//可能接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
p.Start();//启动程序
//向CMD窗口发送输入信息:
p.StandardInput.WriteLine(str1);
p.StandardInput.WriteLine(str2);
//p.WaitForExit();
p.StandardInput.WriteLine("exit");
//获取CMD窗口的输出信息:
string stt = p.StandardError.ReadToEnd();
if (stt == "")
{
stt = "编译成功";
}
//p.StandardInput.WriteLine("exit");
// p.WaitForExit();
p.Close();
return stt;
}
public string cmd1(string str1, string str2)
{
Process p = new Process();
p.StartInfo.FileName = "cmd.exe";//要执行的程序名称
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;//可能接受来自调用程序的输入信息
p.StartInfo.RedirectStandardOutput = true;//由调用程序获取输出信息
p.StartInfo.RedirectStandardError = true;
p.StartInfo.CreateNoWindow = true;//不显示程序窗口
p.Start();//启动程序
//向CMD窗口发送输入信息:
p.StandardInput.WriteLine(str1);
p.StandardInput.WriteLine(str2);
//p.WaitForExit();
p.StandardInput.WriteLine("exit");
//获取CMD窗口的输出信息:
string stt = p.StandardOutput.ReadToEnd();
//p.StandardInput.WriteLine("exit");
// p.WaitForExit();
p.Close();
return stt;
}
private void button3_Click(object sender, EventArgs e)
{
string str1 = "cd /d " + txtfilename.Replace(@"\" + txtFileName, "");
string str2 = "java " + txtFileName.Replace(".java", "");
string str = cmd1(str1, str2);
int i = str.IndexOf(str2, 0);
string s;
if (txtfilename.Replace(@"\" + txtFileName, "").Length == 2)
{
s = str.Substring(i + str2.Length).Replace(txtfilename.Replace(@"\" + txtFileName, "") + @"\>exit", "");
}
else
{
s = str.Substring(i + str2.Length).Replace(txtfilename.Replace(@"\" + txtFileName, "") + @">exit", "");
}
string show = "--------运行结果-----------";
richTextBox2.Text = show + s;
}
private void button4_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "Java文件|*.java";
if (openfile.ShowDialog() == DialogResult.OK)
{
richTextBox1.LoadFile(openfile.FileName,
RichTextBoxStreamType.PlainText);
}
try
{
FileInfo fileInfo = new FileInfo(openfile.FileName);
txtfilename = openfile.FileName;
txtFileName = fileInfo.Name;
}
catch
{
return;
}//加入try和catch为了防止未保存或未打开直接叉掉对话框所带来的错误;
}
private void Form1_Load(object sender, EventArgs e)
{
textBox1.Text = "在用这个小工具之前先安装和配置好JDK,记得先保存再编译运行。 ——cjfl1234";
}
}
}
http://s7/middle/673ccb5bx914aac91f596&690
http://s7/middle/673ccb5bx914aad1a8b36&690
http://s9/middle/673ccb5bx914aad81de48&690