A recent customer request to export a tree directory to the structure of a folder set folder and provide the download function, just begin to feel the function is easier to realize in the shortest time to get the basic functions, when published to the server after the discovery directly in the application of the directory (with Ntko documents and accessories) and then compression, The program is jammed directly! Then think of a window to do a service, but the customer gives too short a time, no way to write a console program to generate an EXE file, and then call this EXE file, so you can ease the pressure of this program!
Write a test project below
Pass the path of a folder to be compressed in the calling EXE end, then get the path under the console to compress it, then return the compressed folder path after the compression is completed or return to a successful state
1. Return path
Copy Code code 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 and help";/the parameter is separated by a space, and if a parameter is empty, you can pass in the
P.start ();
p.WaitForExit ();
Here you can return a string, which is a file path after the successful compression
String output = P.standardoutput.readtoend ();
This. TextBox1.Text = output;
Under the console code:
Copy Code code as follows:
static void Main (string[] args)
{
Receive the sent file path to be compressed
String url = "";
Returns the file path after compression
String returl = "";
if (!string. IsNullOrEmpty (Args[0])
{
url = args[0];
}
A DLL using Ionic.zip is referenced here for a compressed file;
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 state
Copy Code code as follows:
Process myprocess = new process ();
string fileName = path;
String para = "Hello north latitude 30 degrees!" ";
ProcessStartInfo myprocessstartinfo = new ProcessStartInfo (FileName, para);
Myprocess.startinfo = Myprocessstartinfo;
Myprocess.start ();
while (!myprocess.hasexited)
//{
Myprocess.waitforexit ();
//}
int returnvalue = Myprocess.exitcode;
Under the control table
Copy Code code as follows:
static int Main (string[] args)
{
int i=0;
if (!string. IsNullOrEmpty (Args[0])
{
URL = args[0];
}
if (URL)
{
i=1 ;
}
return i;
}