C # calling external files

Source: Internet
Author: User
Tags visual studio 2010

Unfortunately, the software "Data Mining-overlapping clustering" that I used to help my teacher in the past really feels good and has patent rights .. O (zookeeper) O .. Unfortunately, after a long time, I accidentally restored the system and lost it... So it's called regret .. So .. It is necessary to record, think, and summarize a project online ..


I had nothing to do over the past two days and made a desktop software. Some external programs are called. Summary:

1. C # Call the EXE file

System. Diagnostics. process. Start ("calc.exe ");
// This will enable it. Of course, you can handle Fault Tolerance later.

String _ Path = application. startuppath; above is the path to the program you are currently executing
Input. String dfgdf = _ path + "\ general.txt"; with "_ path", you can process the entire string.

2. How to call an external dos program in C?

Use the process object:
System. Diagnostics. PROCESS p = new system. Diagnostics. Process ();
P. startinfo. filename = "arj.exe"; // name of the program to be started
P. startinfo. Arguments = "-x sourcefile. ARJ c: \ Temp"; // startup Parameter
P. Start (); // start
If (P. hasexisted) // determines whether the running is completed.
P. Kill ();

3. Read the word content and display it in the control.

Add a button and a RichTextBox to form, and add a click event to the button.
Private void button#click (Object sender, eventargs E)
{
Object filepath = "d :\\ debugtest.doc ";
Openword (filepath );
}
// Put the word
Extract the document content and put it in RichTextBox.
Private void openword (Object Spath)
{
Object File = Spath;
Object nullobj = system. reflection. Missing. value;
Word. document Doc = mywordapp. documents. open (ref file, ref nullobj, ref upload, ref nullobj, ref nullobj, ref nullobj, ref nullobj );
Doc. activewindow. selection. wholestory ();
Doc. activewindow. selection. Copy ();
Idataobject DATA = clipboard. getdataobject ();
This. richtextbox1.text = data. getdata (dataformats. Text). tostring ();
Doc. Close (ref nullobj, ref nullobj, ref nullobj );
}

4. C # load the flash Control

For example, we will consider how to embed flash into the winform program. In Visual Studio 2010, there is a default unenabled component, Shockwave Flash Object, which can load the Flash program, to enable Shockwave Flash Object, open the toolbox in the upper left corner, right-click the toolbox, select the Add tab, name it AAA, right-click the AAA tab, and select "select item ", you can open the "select toolbox items" dialog box, click "COM components", and drag the scroll bar to the bottom. You will see the Shockwave Flash Object component, select it, and click OK ,:
 
At this point, the Shockwave Flash Object component has been added. This component is displayed on the AAA tab of the toolkit. Then, drag the component into the winform design view and set the size to 258x192, drag a button control and a label control in the public control tab of the toolkit, and add the click event processing function to the button, such:
Press F7 to enter the code editor. The Code is as follows:
 
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
 
Using system. LINQ;
 
Using system. text;
 
Using system. Windows. forms;
 
Using system. xml;
 
 
 
Namespace windowsformsapplication1
 
{
 
Public partial class form1: Form
 
{
 
Public form1 ()
 
{
 
Initializecomponent ();
 
Label1.text = "button ";
 
Axshockwaveflash1.movie = application. startuppath + "\ callapp.swf"; // The callapp.swf file under the same region
 
// Each time Flash calls a function in C #, there is an event response in C #, which is received by axshockwaveflash1.
 
Axshockwaveflash1.flashcall + = new axshockwaveflashobjects. _ ishockwaveflashevents_flashcalleventhandler (axshockwaveflash1_flashcall );
 
}
 
Private void axshockwaveflash1_flashcall (Object sender, axshockwaveflashobjects. _ ishockwaveflashevents_flashcallevent e) // send a request to the
 
{
 
Xmldocument document = new xmldocument ();
 
// The communication between flash and C # is actually mutual data transmission. The data format is XML and stored in the request attribute of the event object.
 
Document. loadxml (E. request );
 
// Obtain and parse the information
 
Xmlattributecollection attributes = Document. firstchild. attributes;
 
 
 
// Get Function
 
String command = attributes. Item (0). innertext;
 
 
 
// Get Parameters
 
Xmlnodelist list = Document. getelementsbytagname ("arguments ");
 
String Arg = List. Item (0). innertext;
 
 
 
Switch (command)
 
{
 
Case "callcsharp ":
 
Callcsharp (ARG );
 
Break;
 
}
 
}
 
 
 
Private void button#click (Object sender, eventargs E)
 
{
 
Callfunction ("output", "Why? "); // Call the output () function in AS, why? Is a parameter
 
}
 
 
 
Private void callfunction (string funname, string Arg)
 
{
 
// C # value passed to flash
 
Axshockwaveflash1.callfunction ("<invoke name = \" "+ funname +" \ "returntype = \" XML \ "> <arguments> <string>" + Arg + "</string> </arguments> </invoke> ");
 
}
 
 
 
Public void callcsharp (string _ Str)
 
{
 
Label1.text = _ STR;
 
Axshockwaveflash1.setreturnvalue ("<string>" + _ STR + "</string>"); // C # value returned to flash
 
}
 
}
 
}


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.