C# .net system.diagnostics.process

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    C# .net system.diagnostics.process

    Hi all i am trying to compile a set of source files located in location In the following code sample i am trying to compile a visual c++ 6.0 (dsp) project from a c# application



    Code:
     ProcessStartInfo procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", @"/c  set path=%programfiles%\Microsoft Visual Studio\Common\MSDev98\Bin");
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute = false;
                System.Diagnostics.Process proc = new System.Diagnostics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                
                proc.WaitForExit();
    
                procStartInfo = new System.Diagnostics.ProcessStartInfo("cmd", @"/c  MSDEV C:\Shared\Source\Binaries\Project\Project.dsp /MAKE  /REBUILD >> compilerr.txt");
                proc.StartInfo = procStartInfo;
                procStartInfo.RedirectStandardOutput = true;
                procStartInfo.UseShellExecute =false;
                
                proc.Start();
    But for the first process(set path) it gives error :

    'proc.ExitCode' threw an exception of type 'System.Invalid OperationExcept ion'

    and for the msdev command :

    'proc.BasePrior ity' threw an exception of type 'System.Invalid OperationExcept ion'


    Any idea where i could be going wrong ?

    Thanks a lot ,

    Rhitam
  • Plater
    Recognized Expert Expert
    • Apr 2007
    • 7872

    #2
    I don't think you can use the output operators >> << | inside the arguments of the process class can you?
    The lines of code that threw the exceptions are not included in your sample source code?

    Comment

    • rhitam30111985
      New Member
      • Aug 2007
      • 112

      #3
      Originally posted by Plater
      I don't think you can use the output operators >> << | inside the arguments of the process class can you?
      The lines of code that threw the exceptions are not included in your sample source code?


      While stepping through the code ... the IDE (Visual c# express) is not throwing any exception .. but if i mouse over the process variable (proc) ... and see its members .. then i get this exception ... it is thrown at each proc.start() line in the sample code ..

      as for the output operators .. thats secondary. . i am not even able to execute the set path command ..

      Comment

      • rhitam30111985
        New Member
        • Aug 2007
        • 112

        #4
        I tried only the set path option like this :

        Code:
         System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo(@"set path=%programfiles%\Microsoft Visual Studio\Common\MSDev98\Bin");
                    psi.UseShellExecute = false;
                    psi.RedirectStandardOutput = true;
                    psi.RedirectStandardInput = true;
                    psi.RedirectStandardError = true;
                    System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
        then it gives the error :

        The system cannot find the file specified win32 exception was unhandled

        stacktrace:


        at System.Diagnost ics.Process.Sta rtWithCreatePro cess(ProcessSta rtInfo startInfo)
        at System.Diagnost ics.Process.Sta rt()
        at System.Diagnost ics.Process.Sta rt(ProcessStart Info startInfo)
        at GetBuildSource. Program.Main(St ring[] args) in C:\Shared\Test Environment\Get BuildSource\Pro gram.cs:line 39
        at System.AppDomai n._nExecuteAsse mbly(Assembly assembly, String[] args)
        at System.AppDomai n.ExecuteAssemb ly(String assemblyFile, Evidence assemblySecurit y, String[] args)
        at Microsoft.Visua lStudio.Hosting Process.HostPro c.RunUsersAssem bly()
        at System.Threadin g.ThreadHelper. ThreadStart_Con text(Object state)
        at System.Threadin g.ExecutionCont ext.Run(Executi onContext executionContex t, ContextCallback callback, Object state)
        at System.Threadin g.ThreadHelper. ThreadStart()

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Originally posted by rhitam30111985
          While stepping through the code ... the IDE (Visual c# express) is not throwing any exception .. but if i mouse over the process variable (proc) ... and see its members .. then i get this exception ... it is thrown at each proc.start() line in the sample code ..

          as for the output operators .. thats secondary. . i am not even able to execute the set path command ..
          Haha, yeah of course they will. You cannot look at an exitcode if thr process has not exited yet, it just means the value is like null still.

          As for why SET doesn't work, its because SET is not a command. There are other ways to set the environment variables for a program. And even if you could set that, it would only apply for the scope of that process, which would end after the set command was fired. So it would not be valid for the next Process object.

          For your actual process object, look at .EnvironmentVar iables on the ProcessStartObj ect

          Comment

          • rhitam30111985
            New Member
            • Aug 2007
            • 112

            #6
            Originally posted by Plater
            Haha, yeah of course they will. You cannot look at an exitcode if thr process has not exited yet, it just means the value is like null still.

            As for why SET doesn't work, its because SET is not a command. There are other ways to set the environment variables for a program. And even if you could set that, it would only apply for the scope of that process, which would end after the set command was fired. So it would not be valid for the next Process object.


            i see .... . will look into it.. thanks a lot :)

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              Something like this maybe? You might have to expand out the %programfiles% string with procStartInfo.E nvironmentVaria bles["%programfiles% "], but I don't think so
              [code=c#]
              System.Diagnost ics.ProcessStar tInfo procStartInfo = new System.Diagnost ics.ProcessStar tInfo("cmd", @"/c MSDEV C:\Shared\Sourc e\Binaries\Proj ect\Project.dsp /MAKE /REBUILD >> compilerr.txt") ;
              procStartInfo.E nvironmentVaria bles["path"]=procStartInfo. EnvironmentVari ables["path"]+@";%programfil es%\Microsoft Visual Studio\Common\M SDev98\Bin;";
              procStartInfo.U seShellExecute = false;
              System.Diagnost ics.Process proc = new System.Diagnost ics.Process();
              proc.StartInfo = procStartInfo;
              proc.Start();
              [/code]

              Comment

              • rhitam30111985
                New Member
                • Aug 2007
                • 112

                #8
                Originally posted by Plater
                Something like this maybe? You might have to expand out the %programfiles% string with procStartInfo.E nvironmentVaria bles["%programfiles% "], but I don't think so
                [code=c#]
                System.Diagnost ics.ProcessStar tInfo procStartInfo = new System.Diagnost ics.ProcessStar tInfo("cmd", @"/c MSDEV C:\Shared\Sourc e\Binaries\Proj ect\Project.dsp /MAKE /REBUILD >> compilerr.txt") ;
                procStartInfo.E nvironmentVaria bles["path"]=procStartInfo. EnvironmentVari ables["path"]+@";%programfil es%\Microsoft Visual Studio\Common\M SDev98\Bin;";
                procStartInfo.U seShellExecute = false;
                System.Diagnost ics.Process proc = new System.Diagnost ics.Process();
                proc.StartInfo = procStartInfo;
                proc.Start();
                [/code]

                procStartInfo.E nvironmentVaria bles["%programfiles% "] returns blank value .. hence the path had to be hardcoded ..even %systemroot% is not working ... apart from that it works like a charm :-)

                Comment

                Working...