在C#程序编译另一个程序的实现方法

开发 后端
在VS2005中使用devenv.exe可以实现在C#程序编译另一程序,本文将结合具体事例向您介绍这种方法的实现过程。

1、C#程序编译编译前,要用到VS2005提供的一个编译工具 devenv.exe,这个在VS安装目录\Common7\IDE 下可以使用控制台命令:

  1. devenv /rebuild debug "【sln path】" 

2、C#程序编译在代码中要运行指定的程序,可以使用process方法:

  1. using System.Diagnostics  
  2. System.Diagnostics.Process.StartInfo   
  3. info=new StartInfo();   
  4. info.FileName="你想要用的特定的程序";   
  5. Info.Arguments="要打开的文件:;   
  6. System.Diagnostics.Process.Start(info);   

3、在C#程序编译的实际使用过程中会遇到一个问题:各人VS安装目录不同,程序中调用devenv的路径就不同。解决这个问题的方法就是读取注册表中的vs的ApplicationPath键值(这个在前面有过介绍)综上,实现方法如下:

  1. using System.Diagnostics;  
  2. using Microsoft.Win32;  
  3.  
  4. private const string REGISTKEY ="  
  5. SOFTWARE\\Microsoft\\MSEnvCommunityContent  
  6. \\ContentTypes\\Addin\\ContentHosts\\  
  7. 1.0\\Visual Studio 2005";  
  8.  
  9. RegistryKey rkey = Registry.LocalMachine;  
  10. //The second parameter tells it to open   
  11. the key as writable  
  12. RegistryKey rkey1 = rkey.OpenSubKey  
  13. (REGISTKEY, true);  
  14. string visualStudio8Path = rkey1.GetValue(  
  15. "ApplicationPath").ToString();  
  16.  
  17. ProcessStartInfo startInfo =   
  18. new ProcessStartInfo(visualStudio8Path);  
  19. startInfo.Arguments = "/rebuild debug " 
  20.                 + "【sln path】";  
  21.  
  22. Process process = new Process();  
  23. process.StartInfo = startInfo;  
  24.  
  25. process.Start();  
  26. process.WaitForExit(); 

【编辑推荐】

  1. C#中定义装箱和拆箱详解
  2. 浅谈C#类型系统
  3. 三种不同的C#异常类型
  4. 详细介绍C#编译器
  5. C#异常机制的相关解释
责任编辑:冰荷 来源: cnblogs
相关推荐

2009-08-04 15:18:11

C# Actor

2016-12-26 15:23:21

戴尔

2009-08-25 01:46:00

C# WINDOWS服

2009-09-11 09:11:09

2017-12-11 13:17:37

2009-07-31 17:14:19

C#语言Web程序

2009-08-07 17:32:17

C#编译程序

2011-03-28 14:02:07

MirahJava对手

2012-01-12 10:09:55

Elementary 思路

2009-04-23 09:42:39

FubuMVCASP.NET MVCMVC

2011-07-20 10:02:01

Xcode cocoa 窗口

2009-07-22 17:15:04

C#实现

2009-08-31 17:13:09

2011-11-10 09:46:41

云计算云管理

2023-12-07 07:26:04

2022-11-02 08:51:01

2021-05-29 07:13:26

微软Nobelium网络攻击

2024-01-15 00:35:23

JavaScript框架HTML

2018-12-05 09:00:46

DevOps持续交付持续集成

2021-06-16 12:03:49

WindowsLinux游戏
点赞
收藏

51CTO技术栈公众号