使用CMD和CSC编译C#

以下列出两种方法:
方法一,使用win api


using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace CSCTest
{
    public enum ShowCommands
    {
        Hide = 0,
        ShowNormal = 1,
        ShowMinimized = 2,
        ShowMaximized = 3,
        Maximize = 3,
        ShowNormalNoActivate = 4,
        Show = 5,
        Minimize = 6,
        ShowMinNoActivate = 7,
        ShowNoActivate = 8,
        Restore = 9,
        ShowDefault = 10,
        ForceMinimized = 11
    }
    public class WindowsAPI
    {
        [DllImport("shell32.dll")]
        public static extern IntPtr ShellExecute(
            IntPtr hwnd,
            string lpOperation,
            string lpFile,
            string lpParameters,
            string lpDirectory,
            ShowCommands nShowCmd);
    }



    class Program
    {
        static void Main(string[] args)
        {
            
            //string strCMD = @"F:\cscTest\complier.bat";
            WindowsAPI.ShellExecute(IntPtr.Zero, "open", strCMD, "", "", ShowCommands.Hide);
            

            Console.ReadLine();
        }
      
    }

}


cd F:\csctest
F:
csc.exe /out:SayHello.exe /target:winexe Hello.cs /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationFramework.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\WindowsBase.dll" /reference:"C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationCore.dll"

 

Hello.cs的内容如下

using System;
using System.Windows;
namespace zzm.SayHello
{
    class SayHello 
    {
        [STAThread]
        public static void Main()
        {
            Window win = new Window();
            win.Height = 200;
            win.Width = 300;
            win.Title = "你好,WPF!";
            win.Show();


            Application app = new Application();
            app.Run();

         }

     }
}


方法二,使用C#的CMD:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace CSCTest
{
   
    class Program
    {
        static void Main(string[] args)
        {
            
            
            MyBatCommand();
            Console.ReadLine();
        }
        public static void MyBatCommand()//名称
        {
   //如下的三个字符串,代表三条批处理命令
            string MyDosComLine1, MyDosComLine2, MyDosComLine3;
            const string cscCmd = "csc.exe /out:SayHello.exe /target:winexe Hello.cs ";
            const string rpath1 = @"/reference:""C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationFramework.dll"" ";
            const string rpath2 = @"/reference:""C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\WindowsBase.dll"" ";
            const string rpath3 = @"/reference:""C:\Program Files (x86)\Reference Assemblies\Microsoft\Framework\.NETFramework\v3.5\Profile\Client\PresentationCore.dll""";
          MyDosComLine1 = @"cd F:\csctest";//返回根目录命令
            MyDosComLine2 =  "F:";
            MyDosComLine3 =  cscCmd + rpath1 + rpath2 + rpath3;//将当前目录所有文件复制粘贴到E盘

            Process myProcess = new Process();
            myProcess.StartInfo.FileName = "cmd.exe ";//打开DOS控制平台 
            myProcess.StartInfo.UseShellExecute = false;
            myProcess.StartInfo.CreateNoWindow = false;//是否显示DOS窗口,true代表隐藏;
            myProcess.StartInfo.RedirectStandardInput = true;
            myProcess.StartInfo.RedirectStandardOutput = true;
            myProcess.StartInfo.RedirectStandardError = true;
            myProcess.Start();
            StreamWriter sIn = myProcess.StandardInput;//标准输入流 
            sIn.AutoFlush = true;
            StreamReader sOut = myProcess.StandardOutput;//标准输入流 
            StreamReader sErr = myProcess.StandardError;//标准错误流 

            sIn.Write(MyDosComLine1 + System.Environment.NewLine);//第一条DOS命令 
            sIn.Write(MyDosComLine2 + System.Environment.NewLine);//第二条DOS命令 
            sIn.Write(MyDosComLine3 + System.Environment.NewLine);//第三条DOS命令
            sIn.Write("exit" + System.Environment.NewLine);//第四条DOS命令,退出DOS窗口

            string s = sOut.ReadToEnd();//读取执行DOS命令后输出信息 
            string er = sErr.ReadToEnd();//读取执行DOS命令后错误信息 
            Console.WriteLine(s);
            Console.WriteLine(er);
            if (myProcess.HasExited == false)
            {
                myProcess.Kill();
                //MessageBox.Show(er);
            }
            else
            {
                //MessageBox.Show(s);
            }

            sIn.Close();
            sOut.Close();
            sErr.Close();
            myProcess.Close();
        }

    }
}


 



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值