在C#程序編譯另一個(gè)程序的實(shí)現(xiàn)方法
作者:佚名
在VS2005中使用devenv.exe可以實(shí)現(xiàn)在C#程序編譯另一程序,本文將結(jié)合具體事例向您介紹這種方法的實(shí)現(xiàn)過程。
1、C#程序編譯編譯前,要用到VS2005提供的一個(gè)編譯工具 devenv.exe,這個(gè)在VS安裝目錄\Common7\IDE 下可以使用控制臺(tái)命令:
- devenv /rebuild debug "【sln path】"
2、C#程序編譯在代碼中要運(yùn)行指定的程序,可以使用process方法:
- using System.Diagnostics
- System.Diagnostics.Process.StartInfo
- info=new StartInfo();
- info.FileName="你想要用的特定的程序";
- Info.Arguments="要打開的文件:;
- System.Diagnostics.Process.Start(info);
3、在C#程序編譯的實(shí)際使用過程中會(huì)遇到一個(gè)問題:各人VS安裝目錄不同,程序中調(diào)用devenv的路徑就不同。解決這個(gè)問題的方法就是讀取注冊(cè)表中的vs的ApplicationPath鍵值(這個(gè)在前面有過介紹)綜上,實(shí)現(xiàn)方法如下:
- using System.Diagnostics;
- using Microsoft.Win32;
- private const string REGISTKEY ="
- SOFTWARE\\Microsoft\\MSEnvCommunityContent
- \\ContentTypes\\Addin\\ContentHosts\\
- 1.0\\Visual Studio 2005";
- RegistryKey rkey = Registry.LocalMachine;
- //The second parameter tells it to open
- the key as writable
- RegistryKey rkey1 = rkey.OpenSubKey
- (REGISTKEY, true);
- string visualStudio8Path = rkey1.GetValue(
- "ApplicationPath").ToString();
- ProcessStartInfo startInfo =
- new ProcessStartInfo(visualStudio8Path);
- startInfo.Arguments = "/rebuild debug "
- + "【sln path】";
- Process process = new Process();
- process.StartInfo = startInfo;
- process.Start();
- process.WaitForExit();
【編輯推薦】
責(zé)任編輯:冰荷
來源:
cnblogs