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;
}