现在小弟有这样一个想法:在只安装.net 2.0的情况下做一个自动编译运行C#源代码的程序。
界面如下:
在点下查找按钮的时候打开一个“打开文件对话框”来选择文件,并对文件的路径做相应的处理。
在殿下编译按钮的时候自动打开一个控制台,来运行以下一组操作:
1. 设置选择文件的目录为控制台的当前目录。
2. 编译选择的文件,并把编译信息现在在打开的控制台窗口中。
3. 如果有错误,暂停程序运行,并把编译错误信息显示在控制台窗口中。如果没有错误,程序接着运行编译好的程序,并把运行信息也显示在这个控制台窗口中。
4. 显示完上述信息之后,控制台暂停运行,给用户查看信息的时间。这个时候用户可以点击任意键退出。到这里,这个小软件的功能就全部介绍完毕了。这个想法是我在网易学院看了陈广老师的“C#参考视频”之后,自己也想做一个演示中陈广老师那样的小软件。但是,到此为之我就卡壳了,在点击编译按钮之后总是出现各种各样莫名其妙的问题。现在,我贴出来自己的代码。希望高手能够指点一下,和我一样的新手,咱们共同进步。/*
 * Created by SharpDevelop.
 * User: Lenic
 * Date: 2007-11-16
 * Time: 23:38
 *
 */using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;namespace CSharp_Compiler
{
/// <summary>
/// Description of MainForm.
/// </summary>
public partial class MainForm : Form
{
public MainForm()
{
//
// The InitializeComponent() call is required for Windows Forms designer support.
//
InitializeComponent(); //
// TODO: Add constructor code after the InitializeComponent() call.
//
} string dir = string.Empty;//获得原始文件路径
string name = string.Empty;//获得原始文件的名字
string name_exe = string.Empty;//可执行文件的名字 void BtnOKClick(object sender, EventArgs e)
{
openFileDialog1.Title = "打开文件";
openFileDialog1.Multiselect = false;
openFileDialog1.Filter = "C#文件(*.cs)|*.cs";
openFileDialog1.FilterIndex = 1;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
dir = Path.GetDirectoryName(openFileDialog1.FileName);
name = Path.GetFileName(openFileDialog1.FileName);
name_exe = Path.GetFileNameWithoutExtension(openFileDialog1.FileName)+".exe";
}
} void BtnCompileClick(object sender, EventArgs e)
{
if (dir == string.Empty)
            {
                MessageBox.Show("还没有选择文件!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
{
             Process p = new Process();
                p.StartInfo.WorkingDirectory = dir;
                p.StartInfo.FileName = "cmd";
                p.StartInfo.Arguments = @"/c ";
                p.StartInfo.UseShellExecute = false;
                p.StartInfo.CreateNoWindow = true;
                p.StartInfo.RedirectStandardInput = true;
                p.StartInfo.RedirectStandardOutput = true;
                p.StartInfo.RedirectStandardError = true;
                p.Start();
                p.StandardInput.WriteLine("csc " + name);
                p.Close();                //Process pro = new Process();
                //pro.StartInfo.FileName = "cmd";
                //pro.StartInfo.WorkingDirectory = dir;
                //pro.StartInfo.RedirectStandardInput = true;
                //pro.StandardInput.Write(tmp);
                ////pro.StandardInput.WriteLine(name_exe);
                //pro.Start();
                ////pro.WaitForExit();
                //pro.Close();
            }
}
}
}