CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2006
    Location
    Rhode Island
    Posts
    32

    [RESOLVED] Run cmdline code with C#

    I'm trying to eliminate our dependence on batch files for compiling our many projects. I am forced to use a huge batch file to create our installation and am just tired of all my co-workers' questions about how to compile certain projects. So I'm trying to create an application in C# that will dynamically compile, register, delete, etc the projects that a specific project.

    The problem I'm having is that I cannot execute commandline code from C#. I used the examples that I've found on MSDN, CodeGuru, etc. and below is what I've come up with. I've used the code from the batch file that is supposed to usable by commandline. But when I run the code below all I get is a cmd window that points to the debug folder of the project itself (not the one I need compiled).

    Here's most of my code, or at least the portion that will execute the commandline:
    Code:
    //--- create process member
    System.Diagnostics.Process compilerProcess;
    compilerProcess = new System.Diagnostics.Process();
    compilerProcess.EnableRaisingEvents = false;
    compilerProcess.StartInfo.FileName = @"C:\\windows\\system32\\CMD.exe";
    compilerProcess.StartInfo.Arguments = "C:\\Program Files\\Microsoft Visual Studio\\COMMON\\msdev98\\Bin\\msdev.exe c:\\cql\\source\\mainline\\souce\\create\\cql.dsw /MAKE cql_ii - Win32 Release";
    compilerProcess.StartInfo.UseShellExecute = false;
    compilerProcess.StartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
    compilerProcess.StartInfo.CreateNoWindow = true;
    compilerProcess.StartInfo.RedirectStandardOutput = true;          
    compilerProcess.Start();
    compilerProcess.WaitForExit();
    rtfOutput.Text = compilerProcess.StandardOutput.ReadToEnd().ToString();
    compilerProcess.Close();
    Any help in this is appreciated...
    Last edited by DemonPiggies; February 4th, 2011 at 12:45 PM. Reason: Forgot code tags, sorry...

  2. #2
    Join Date
    Jun 2009
    Posts
    144

    Re: Run cmdline code with C#

    try this

    Code:
    using System.IO;
    .....
    
                ProcessStartInfo ProcessInfo;
                Process Process;
                ProcessInfo = new ProcessStartInfo(@"C:\windows\system32\cmd.exe", @"""C:\Program Files\Microsoft Visual Studio\COMMON\msdev98\Bin\msdev.exe"" ""c:\cql\source\mainline\souce\create\cql.dsw /MAKE cql_ii - Win32 Release""");
                ProcessInfo.CreateNoWindow = true;
                ProcessInfo.UseShellExecute = false;
                Process = Process.Start(ProcessInfo);
                Process.Close();
    there is no need for \\ , when you use paths like this @"your path here"
    Last edited by invader7; February 3rd, 2011 at 06:10 PM.

  3. #3
    Join Date
    Apr 2006
    Location
    Rhode Island
    Posts
    32

    Re: Run cmdline code with C#

    So apparently I misspelled the file's path which was problem #1.
    Problem #2 is that I couldn't get invader7's code to work correctly which is my fault.
    I needed quotation marks around the compiler type.
    For whatever reason, I can't call C:\Program Files\Microsoft Visual Studio\COMMON\msdev98\Bin\msdev.exe directly but I can use msdev though. I'm not sure why I can't call msdev.exe as a parameter of cmd.exe using C#. Let me clarify real quick. I can call msdev.exe through cmd.exe and somehow I can pass in the file path too but I could [b]not[b] get it to compile by sending the Make command as a parameter in the same string. Maybe it's too many parameters for cmd.exe? Or I'm calling it wrongly?

    Anyway here's the correct code (because I hate when people say things are solved but never show their answer):
    Code:
    ProcessStartInfo startInfo = new ProcessStartInfo("msdev");
    startInfo.Arguments = @"""c:\cql\source\mainline\source\create\cql.dsw"" /Make ""cql_ii - Win32 debug""";
    Process.Start(startInfo);
    Last edited by DemonPiggies; February 4th, 2011 at 12:44 PM. Reason: added clarification...

  4. #4
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Run cmdline code with C#

    Have you thought about using any one of the multiple applications already available for organizing builds?

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Run cmdline code with C#

    VS has a built in setup. If you want scripting, look at Powershell 2.0
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured