The release number of this article has been CHS305994
This article references the following Microsoft .NET Framework Class Bank Name Space:
System.io system.diagnostics
This task content
summary
Generating packaging code Using packages in applications
SUMMARY This article will gradually describe how to write packaging for the command line utility that you want to use as an automated object in the Microsoft .NET framework with Visual Studio.
To successfully generate a package for the command line utility used as an automated object, you need to be able to start the process, and then you need to get the execution output of the process. This process is divided into the following parts:
Write code for packaging:
Class newly stored the package. Add internal methods of actual functionality to such storage programs to this class. Use packaging in your app:
Add a class file to a new application you want to use.
Back to top
To generate code for the package to generate a code for the package, first create a category that deposits the code. To do this, use the Visual Studio or text editor (such as "Notepad") to create a new Visual C # .NET application.
Note: In this article, the name of the Visual C # .NET application example is Launchexe.
After creating a new class for storing the code, add an internal method that will actually functionally functionally functionally in the program. If this method is declared as internal, it can be used throughout the program; however, other applications outside the program cannot access it.
Generate code:
Use the USING statement to make a declaration to the compiler (see the first code snippet). Use System.IO and System.Diagnostics resources as follows:
System.io: Read and write to the streamwriter object. System.Diagnostics: Starts an external process from the application. Declare the Launchexe class and declare the public member function run of the Launchexe class. RUN method returns a string to the call. After the string completes its work, the output of Launchexe will be saved. The following list describes three parameters of the RUN function:
The first parameter passed to the Run function is Exename, which is the name of the command line tool application that you will start. This .Exe file must be resigned in the same folder with an application that uses this class in the same folder unless a full path is specified as another parameter. In this article, assuming that the .exe file is located in the same folder with the generated application. The second parameter passed to the Run function is Argsline. This parameter consists of a string that contains the parameters that will be passed to the command line tool. The third parameter definition .EXE file must complete the time required to run and return the result (in seconds). Using system.diagnostics;
Using system.io;
Public Class Launchexe
{
Internal static string run (String Exename, String Argsline, Int Timeoutseconds)
} Prepare the StreamReader object to process the execution output of the command line tool. The StreamReader object performs the following features:
Read the output created by the executable program. Create a string containing the executable output. This output will be sent back to the application. Setting the Boolean value to false, declare that this operation has not been successful. The Boolean value can be set to true after the operation is successfully completed. StreamReader outputstream = streamreader.null;
String Output = ""
Bool success = false; New Process Object, then pass the executable program (launcheeexe), and parameter (argsline) to run the command line utility. In this example, we use the CreateNowindow property to cancel the MS-DOS window when you call your class each time to start the executable. Try {
Process newprocess = new process ();
NewProcess.startinfo.FileName = Exename
NewProcess.startinfo.arguments = arggo.Arguments
NewProcess.startinfo.useshellexecute = false;
NewProcess.startinfo.createnowindow = true;
NewProcess.StartInfo.RedirectStandardOutput = True;
NewProcess.Start (); if the operation does not time, the canceled MS-DOS window from the command line utility is output, then destroy the object: if (0 == Timeoutseconds)
{
OutputStream = newProcess.standardoutput;
Output = OutputStream.readToend ();
NewProcess.waitForexit ();
}
Else
{
Success = newProcess.waitForexit (TimeoutSeconds * 1000);
IF (SUCCESS)
{
OutputStream = newProcess.standardoutput;
Output = OutputStream.readToend ();
}
Else
{
Output = "TIMED OUT AT" TIMEOUTSECONDS "Seconds Waiting for" Exename "To EXIT."
}
}
Catch (Exception E)
{
"New Exception (" An Error Occurred Running " ExEname ". ", e));
}
Finally
{
OutputStream.Close ();
} Return the output to the call method: return "/ t" Output; saves the file as Start.cs. This code example provides a valid approach to packing all command line utilities. In the application you have written, you can call
Launchexe
The RUN method is passed to any executable with the corresponding parameters and timeout values, then analyze the results in your own application.
Back to top
Using the package in the application To use the package, you need to add a class file to the new application that will use this class. You can also use this class in any other Visual C # .NET application.
To integrate such this class into the Visual Studio C # application:
Start Visual Studio. Open the new project dialog. On the File menu, point to New, and then click Project. Select the Visual C # item and select the Windows application as a template. Type the name of the project in the Name box, then click OK. Visual Studio will create a new C # Windows application. In the Solution Explorer, right-click the project name, point to add, and then click Existing. Adding an existing dialog box will open in Visual Studio. To add a C # class file to your application, browse to the folder of Start.cs, right-click Start.cs, and then click Open. To access the class in Start.cs from the application, let your class file be a member of the project namespace: In the Solution Explorer, right-click the form1.cs file created for the project, then In the main window, click View Code to open Form1.cs. At the top of the code file, you should see the Word namespace, followed by the project name. Add this line in the same location in the Start.cs file. In the Solution Explorer, right-click Start.cs, and then click View Code. Add a namespace row after the USING statement. Now your code should be similar to the following example (in this example, the project name is WindowsApplication1): use system;
Using system.io;
Using system.diagnostics;
Namespace WindowsApplication1
{
INTERNAL CLASS LAUNCHEXE
{
Internal static string run (String Exename, String Argsline, Int Timeoutseconds)
{
... Make this file into members of the namespace to create references to class files, and then call the RUN method:
Click the Form1.cs [Design] tab to view your form. Place a button on the form and call the RUN method in the Click event of the button.
On the View menu, click Toolbox. Drag and drop the button control onto the form. Double-click the button to start Visual Studio Creating the code editor used by the event handler, which is used to place the code of the click Click event. It should be placed in a code part similar to the example below: Private Void Button1_Click (Object Sender, System.Eventargs E)
{
} Add the following code to the event handler to call the RUN function: Private void button1_click (Object Sender, System.EventArgs E)
{
String Output;
Output = launchexe.run ("CommandLineTool.exe", "Arg1 Arg2", 10);
System.windows.Forms.MessageBox.show (Output);
} The first line of this code, you declare a new variable named Output for storing the result of the command line tool. In the second line, you declare that Output variables are equal to the results returned by the RUN method. In this line, you also specify to start CommandLineTool.exe (you can replace the program to your own command line tool). Passing two parameters arg1 and arg2 to the command line tool (you can replace these parameters to pass the parameter name to the .exe program). The value of the third parameter is 10, which indicates that the command line tool must be completed within 10 seconds. The last line in this code example creates a pop-up message, displaying the output of the command line tool in the message. Press the F5 key to run the application. Click the button on the form. Within 10 seconds, the pop-up message box should open and display the output of the command line tool. In practical applications, you can analyze the output returned from the command line tool and display the user by a graphical user interface (GUI) in a more friendly form. The information in this article applies to:
Microsoft Visual C # .NET (2002)
Recent Updated: 2002-7-19 (1.0) Keyword KBDiagnostics Kbhowto Kbio KB305994