Click here to Skip to main content
Email Password   helpLost your password?

Sample Image - builditemincontextmenu.jpg

Introduction

This little article can give you a hint on how you can build your solution files quickly without running 6 Visual Studio applications that enlarge your scratch file till your computer is slow like my grandma.

First of all run %ProgramFiles%\Microsoft Visual Studio .NET\Common7\Tools\vsvars32.bat, if devenv.exe (Visual Studio IDE) could not be found from command line.

Command line switches of devenv.exe

To see this list, type devenv /? on command prompt

Console Application BuilderHelper

This little console application is able to check the return value from devenv.exe and we can display the build log if there went something wrong.

The basic method looks like this:

System.Diagnostics.ProcessStartInfo is filled with needed information.

public int Build(string solutionFile, string solutionConfig)
{
    // get temp logfile path

    string logFileName = System.IO.Path.GetTempFileName();
    // populate process environment

    System.Diagnostics.ProcessStartInfo psi = 
        new System.Diagnostics.ProcessStartInfo();
    psi.FileName =@"devenv.exe";
    psi.ErrorDialog = true;
    psi.Arguments =  "\"" +solutionFile +"\"" +  
         @" /rebuild "+ solutionConfig 
         + " /out " + logFileName;

Now we start the process and wait for exit.

// start process

System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
// instruct process to wait for exit

p.WaitForExit();
// get return code

int exitCode = p.ExitCode;
// free process resources

p.Close();

If devenv.exe did not return zero, we display the build log to the user.

// if there was a build error, display build log to console

if (exitCode != 0)
{
    System.IO.TextReader reader =  System.IO.File.OpenText(logFileName);
    errorLog = reader.ReadToEnd();
    reader.Close();
    //

    System.Console.WriteLine(errorLog);
    System.Console.WriteLine("Hit enter to abort...");
    System.Console.Read();
}
// delete temp logfile

System.IO.File.Delete(logFileName);

Last but not least, we return the code.

// return process exit code

    return exitCode;
}

Build Batch

Extend context menu with build item

File Extension Dialog

Have phun with it...

Revision History

16.12.2002

14.12.2002

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
QuestionDoes anybody know how to update web reference in a solution within command line?
gusenica
3:30 25 Apr '08  
I have some solution those I want to aumatically rebuild but one of them contains web reference that need to update during rebuild process. Using recommendations in this article it is possible to automate rebuild of all solutions but not web references
GeneralMy Version
eraserhead
9:21 16 Oct '03  
It always prints out the log, and it keeps a time stamp.

I tried to do a realtime log monitor but VS has a lock on the log file so it can't be read.

          public int Build(string solutionFile, string solutionConfig)
          {    
               // get temp logfile path
               string logFileName = System.IO.Path.GetTempFileName();

               Console.WriteLine("Building: " + solutionFile);
               Console.WriteLine("Built Started At: " + DateTime.Now);
               DateTime LStartTime = DateTime.Now;

               // populate process environment
               System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo();
               psi.FileName =@"devenv.exe";
               psi.ErrorDialog = true;
               psi.Arguments =   "\"" +solutionFile +"\"" +   @" /rebuild "+ solutionConfig + " /out " + logFileName;
               // start process
               System.Diagnostics.Process p = System.Diagnostics.Process.Start(psi);
               // instruct process to wait for exit
               p.WaitForExit();
               // get return code
               int exitCode = p.ExitCode;
               // free process resources
               p.Close();

               // if there was a build error, display build log to console              
               System.IO.TextReader reader =   System.IO.File.OpenText(logFileName);
               errorLog = reader.ReadToEnd();
               reader.Close();                   

               System.Console.WriteLine(errorLog);
              
               Console.WriteLine("devenv.exe returned exit code: " + exitCode);
               Console.WriteLine("Build Ended At: " + DateTime.Now);
               Console.WriteLine("Build Took: " + (DateTime.Now - LStartTime).ToString());

               System.Console.WriteLine("Press Enter to Continue...");
               System.Console.Read();

               // delete temp logfile
               System.IO.File.Delete(logFileName);
               // return process exit code
               return exitCode;
          }

GeneralDoes anyone know how to display the build log whilst command line builds are in progress?
[DAve]
1:08 1 May '03  
As the title says... Does anyone know how to display the build log whilst command line builds are in progress?

I find it very annoying to just have a cursor blinking at me (I can see it now) when I really want to know how far along the build is.

I have tried a tail util on the build log, but that does not work.

Anyone any ideas?

DAve


GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress?
normanr
5:37 16 Jul '04  
Basically you have to set the RedirectStandardOutput and RedirectStandardError values of the ProcessStartInfo object to true and then read the StandardOutput and StandardError streams. Make sure this all gets done in a seperate thred otherwise you'll end up locking up the application until devenv finishes.

check out the Launching a process and displaying its standard output article for sample code, et. al.
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress?
[DAve]
5:55 16 Jul '04  
Woh!

That was a reply from the past... I barely remember asking the question Smile

The problem has now vanished since recent versions of .NET output to the console that the command line was run from.

Thanks for your help though that link was very usefull for another thing I'm looking at.

Cheers

DAve
GeneralRe: Does anyone know how to display the build log whilst command line builds are in progress?
M_A_MaDeRo
6:44 25 Jul '05  
I redirected the StandardOutput and StandarError, I have done this with other processes but with devenv I just get "" "" for both objects. If I dont redirect the output I can see the message in the console windows, but cant get it from there.
Here's my code:
//Somewhere else I call it like:
//return base.Compilar("\""+ pathSolutionFile+"\" /build Debug");
protected string Compilar(string arguments)
{
System.Diagnostics.Process process = new Process();

if(pathCompiladorDirectory != null)
{
process.StartInfo.WorkingDirectory = pathCompiladorDirectory;
process.StartInfo.FileName = pathCompiladorDirectory + ejecutable;
}
else process.StartInfo.FileName = ejecutable;
process.StartInfo.Arguments = arguments;
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardError = true;

process.Start();

// Crea una instancia de Teller, nada más para poder tener el proceso en otro Thread.
Teller outputTeller = new Teller(process.StandardOutput);
outputTeller.LeerEvent += new Teller.Leer(outputTeller_LeerEvent);
Thread outputThread = new Thread(new ThreadStart(outputTeller.Lee));
outputThread.Start();

Teller errorTeller = new Teller(process.StandardError);
errorTeller.LeerEvent += new Teller.Leer(errorTeller_LeerEvent);
Thread errorThread = new Thread(new ThreadStart(errorTeller.Lee));
errorThread.Start();

process.WaitForExit();
process.Close();
while(errorThread.IsAlive || outputThread.IsAlive);
return output + System.Environment.NewLine + error;
///aaaaah!!! tanto pedo nada más para evitar deadlocks
}
protected void errorTeller_LeerEvent(StreamReader reader)
{
error = reader.ReadToEnd();
}

protected void outputTeller_LeerEvent(StreamReader reader)
{
output = reader.ReadToEnd();
}

protected class Teller
{
public delegate void Leer(StreamReader reader);
public event Leer LeerEvent;
StreamReader reader;
public Teller(StreamReader reader)
{
this.reader = reader;
}

public void Lee()
{
if(LeerEvent!=null)
LeerEvent(reader);
}
}


The weird thing is that this code is working for other compilers, but not with devenvFrown


Generalproblem with devenv.exe (vs.net)
nagash1
18:01 7 Mar '03  
hi guys!

i got a problem. vs.net doesnt start.
instead i get an error message.
the pop-up window says: "ms development environment has determined a problem und got to be shut down" (sorry, translation from german version)
"an unhandled exception has been caught by the vsw exception filter."
appname:devenv.exe appver:7.0.9466.0 modname: hxds.dll
modver: 2.1.9466.0 offset: 0001e84f
im anything but skilled with this stuff, maybe one of you guys can help me out.
thank you!

GeneralRe: problem with devenv.exe (vs.net)
Jerry Maguire
3:49 8 Mar '03  
Hi,

hätte die deutsche Meldung auch verstanden ,)

could you please give me more detailed information about what you did, i guess you launched the console application via context menu item to build a solution. If you start devenv.exe from command line, everything works as expecteded ? Only if you use the console application it crash ?

.:Greets from Jerry Maguire:.
GeneralRe: problem with devenv.exe (vs.net)
Jerry Maguire
6:06 12 Mar '03  
take a look

.:Greets from Jerry Maguire:.
GeneralRunning from IIS
Nikolay Simeonov
10:03 16 Dec '02  
I have a problem trying to run it from the web server. It's weird - if someone is logged on to the console or using terminal client - it works. When noone is logged on - it doesn't.

I set all the neccessary permissions etc, but unless I log on to the server "devenv.exe" is freezing - not consuming the CPU, but doesn't do anything and the buildlog.txt file is locked until I kill the process.

Do you have any ideas why it happens like that and how can I fix it?
GeneralRe: Running from IIS
Jerry Maguire
12:50 16 Dec '02  
Hi Nikolay,

hmmm, now for every user ( calling ) the buildlog.txt is created in his user profile directory where his permissions are granted.

Did you try to start devenv.exe with a batch call ? If devenv.exe then also freeze, it should not be a problem of the console application. No good solution for you, i know. If you find the error please drop me note, thanks.

I never try run this little thing on a server, just test it on my client.

.:Greets from Jerry Maguire:.
GeneralRe: Running from IIS
Nikolay Simeonov
13:01 16 Dec '02  
The idea is to minimize the uploads neccessary and ease the development over the internet. And I don't want to upload the binaries all the time...

It works perfectly as a batch file.

It works perfectly when I call it from the console application.

It works perfectly when I start the console application from IIS on my machine.

It works perfectly when I start the console application on the server from my machine only while I'm logged on to it. Once I log off and the program stops working... isn't that weird ?

If you or someone knows why it's freezing I'll be very thankful to learn 'why'.

(Eventually if I find a way around or figure out why it happend like that I will let you know) Smile

Once again thanks for the article - helped me a lot ( yes, I keep the connection open all the time now Smile )
GeneralRe: Running from IIS
Jerry Maguire
13:16 16 Dec '02  
Me too ,)

please try it with the updated code.

Have a nice day

.:Greets from Jerry Maguire:.
GeneralProject Path containing SPACEs
CoolDude
3:13 16 Dec '02  
Hi,
i experience problems with the tool if a project path that contains spaces.Cry After changing on line in the code the tools works for me on those project paths.Smile

Orig:
psi.Arguments = solutionFile + @" /rebuild "+ solutionConfig + " /out " + logFileName; Modified:
psi.Arguments = "\"" +solutionFile +"\"" + @" /rebuild "+ solutionConfig + " /out " + logFileName;
Nice tool, thanks and regards, Christian
GeneralRe: Project Path containing SPACEs
Jerry Maguire
4:55 16 Dec '02  
Hi,

thanks for the hint, i will update the code.

Have a nice day

.:Greets from Jerry Maguire:.
GeneralREG File
Ryan Farley
11:07 10 Dec '02  
You could create a REG file to add this to the registry automatically. I've done this so anytime I install VS, I can just execute the REG file to create the context menu items.

Create a text file and add the following text. Then rename the text file to "something.reg". Then to add it to the registry just execute the reg file. This text adds both 'Build Debug' and 'Build Release' to the context menu (using XP reg version 5.00).

Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug]
@="Build Debug"
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug\command]
@="\"C:\\Program Files\\Microsoft Visual Studio .NET\\Common7\\IDE\\devenv.exe\" \"%1\" /rebuild Debug"
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug\ddeexec]

[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug\ddeexec\Application]
@="devenv"
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Debug\ddeexec\Topic]
@="System"
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release]
@="Build Release"
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release\command]
@="\"C:\\Program Files\\Microsoft Visual Studio .NET\\Common7\\IDE\\devenv.exe\" \"%1\" /rebuild Release"
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release\ddeexec]

[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release\ddeexec\Application]
@="devenv"
[HKEY_CLASSES_ROOT\VisualStudio.Solution\Shell\Build_Release\ddeexec\Topic]
@="System"

Anyway, thanks for the article. It is a good idea.

-Ryan
GeneralRe: REG File
Jerry Maguire
13:04 11 Dec '02  
Hi Ryan,

you're right, this is a good reusable solution, like you can see i add a example bat and reg file.

At the time i wrote this article, i got the feeling that most people don't like to "work" on the registry just to extend a context menu item. But it's shure a fast and reusable way to do that.

thanks

.:Greets from Jerry Maguire:.
GeneralGerman Screenshots,
Jerry Maguire
13:14 9 Dec '02  
Sorry for my bad english and the german screenshots, but i had no english XP/VS.NET by the hand.

.:Greets from Jerry Maguire:.
GeneralRe: German Screenshots,
Daniel Turini
22:12 9 Dec '02  
Oh, and your keyboard was not working all right, so you needed to change some 'S' for '$', right? Poke tongue

I see dumb people
GeneralRe: German Screenshots,
Heath Stewart
2:52 10 Dec '02  
I agree totally with your point. Microsoft developers shouldn't complain AND develop with Microsoft technologies. It's like having your cake and squashing it too. This is a really tacky approach and someone might mistake you for a linux junkie...or worse yet, a Mac luser!

"Well, I wouldn't say I've been missing it, Bob." - Peter Gibbons
GeneralRe: German Screenshots,
Jerry Maguire
3:22 10 Dec '02  
Hi,
hmmm.. what should i say, i really like microsoft, and my daily business is C# with Visual Studio. I think your feedback isn't really fair.

.:Greets from Jerry Maguire:.
GeneralRe: German Screenshots,
Anonymous
4:04 10 Dec '02  
I wouldn't worry about it ... too much political correctness has blinded people. And besides, with $36 billion in cash assets I'd say Microsoft was the very definition of ca$h money!!!
GeneralRe: German Screenshots,
Uwe Keim
7:34 10 Dec '02  
Nonsense!

Writing some "M$" rubbish instead of "Microsoft" just shows your childishness, nothing more.


--
- Free Windows-based Web Content Management System: http://www.zeta-software.de/enu/producer/freeware/download.html - Scanned MSDN Mag ad with YOUR name: www.magerquark.de/misc/CodeProject.html - See me: www.magerquark.de
GeneralRe: German Screenshots,
Anonymous
7:36 10 Dec '02  
Oh lighten up Francis. The guy can say whatever he wants. Not everyone tows the line in the same way. You could easily argue that Microsoft's licensing policies are ALL about the $$$. Blah Blah Blah.
GeneralRe: German Screenshots,
Uwe Keim
8:00 10 Dec '02  
So you just confirmed.

--
- Free Windows-based Web Content Management System: http://www.zeta-software.de/enu/producer/freeware/download.html - Scanned MSDN Mag ad with YOUR name: www.magerquark.de/misc/CodeProject.html - See me: www.magerquark.de


Last Updated 14 Dec 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2010