. NET calls the exe file generated in the console, passing parameters and getting the response parameters ideas and code

Source: Internet
Author: User

Recently, the customer requested to export a tree Directory into a folder structure and provide the download function. At the beginning, it was easy to implement the function, and the basic function was completed in the shortest time, after being published to the server, we find that the directory (with ntko documents and attachments) is exported directly in the application and then compressed. The program is stuck! Later I thought of doing a window service, but the time the customer gave me was too short. I couldn't write a console program to generate an exe file, and then call this exe file, this can relieve the pressure on the program!

Write a test project

Upload the path of the folder to be compressed on the exe end, and then obtain the path in the console for compression. After compression, the compressed folder path is returned or the status of success is returned.

1. Return Path
Copy codeThe Code is as follows:
String path = @ "E: \ test project \ 201303 \ TestWindowsService \ ca \ bin \ Debug \ ca.exe ";
String fileName = path;

Process p = new Process ();

P. StartInfo. UseShellExecute = false;

P. StartInfo. RedirectStandardOutput = true;

P. StartInfo. FileName = fileName;

P. StartInfo. CreateNoWindow = true;

P. StartInfo. Arguments = @ "D: \ zhai \ aaa \ placement tutorial"; // The parameters are separated by spaces. If a parameter is blank, you can input ""

P. Start ();

P. WaitForExit ();
// A string can be returned here. In this example, a file path after successful compression is returned.
String output = p. StandardOutput. ReadToEnd ();
This. TextBox1.Text = output;

Code in the console:
Copy codeThe Code is as follows:
Static void Main (string [] args)
{
// Receives the path of the file to be compressed
String url = "";
// Return the compressed file path
String retUrl = "";

If (! String. IsNullOrEmpty (args [0])
{
Url = args [0];
}
// The dll using Ionic. Zip of a compressed file is referenced here;
Using (ZipFile zf = new ZipFile (System. Text. Encoding. Default ))
{
Zf. AddDirectory (url );
Zf. Save (@ "D: \ zhai \ aaa \" + DateTime. Now. ToString ("yyyyMMddHHmmss") + ". zip ");
RetUrl = @ "D: \ zhai \ aaa \" + DateTime. Now. ToString ("yyyyMMddHHmmss") + ". zip ";
}
Console. Write (retUrl );
}

2. Return a status
Copy codeThe Code is as follows:
// Process myProcess = new Process ();

// String fileName = path;
// String para = "Hello, 30 degrees north! ";
// ProcessStartInfo myProcessStartInfo = new ProcessStartInfo (fileName, para );
// MyProcess. StartInfo = myProcessStartInfo;

// MyProcess. Start ();

// While (! MyProcess. HasExited)
//{

// MyProcess. WaitForExit ();

//}
// Int returnValue = myProcess. ExitCode;

Console

Copy codeThe Code is as follows:
Static int Main (string [] args)
{
Int I = 0;
If (! String. IsNullOrEmpty (args [0])
{
Url = args [0];
}
If (url)
{
I = 1;
}
Return I;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.