. NET invoke the EXE file generated under the console, and the idea of getting the return parameter and the code _ practical skill

Source: Internet
Author: User
Tags datetime zip

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

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.