In winform, you sometimes need to open another application.ProgramOr files, such as opening a browser, opening a Word document, opening a folder, and printing a file. This article describes how to use C # To open a new process in winform to complete the above functions.
UsingSystem. Diagnostics
The namespace provides interaction with system processes, event logs, and performance counters. The two basic classes related to processes are system. Diagnostics. process and system. Diagnostics. processstartinfo.
- System. Diagnostics. procss: provides access to local and remote processes, and enables you to start and stop local system processes.
(1) Start (): start the process, mainly including the following parameter settings
Start (processstartinfo)
Start (string filename)
Start (string filename, string arguments)(2) attributes:
ID: unique process ID
Processname: process name
Machinename: name of the computer where the process is running
Startinfo: startinfo OF THE PROCESS
Starttime: the time when the process was started.
Exittime: the time when the process exits.
Hasexited: whether the process has been terminated
- System. Diagnostics. processstartinfo: used with process to set startup parameters for Process
(1) constructor:
Processstartinfo ()
Processstartinfo (string filename)
Processstartinfo (string filename, string arguments)(2) Some attributes:
Filename: application or file name
Arguments: Parameters
Workingdirectory: the initial directory of the startup process.
Createnowindow: whether to start the process in the new window
Windowstyle: Specifies the window opening status (enumeration value)
Verb: the predicate used to open a process. Each file extension name has its own predicate. You can use the verbs attribute to obtain the predicate. For example, the "print" predicate prints the document specified by filename. You can use an empty string ("") to specify the default predicate.
The following uses a winform applet to easily enable the process:
First, introduce the namespace:Using system. Diagnostics;
(1) open a file
Private Void Btnopenfile_click ( Object Sender, eventargs e ){ // Define a processstartinfo instance Processstartinfo psi = New Processstartinfo (); // Set the initial directory of the Startup Process PSI. workingdirectory = application. startuppath; // Set the application or document name of the Startup Process PSI. filename = @" Xwy20151119.txt "; // Set the Startup Process Parameters PSI. Arguments =" "; // Start process resources that contain process startup information Try {Process. Start (PSI );} Catch (System. componentmodel. win32exception ex) {MessageBox. Show (ex. Message ); Return ;}}
(2) open a browser
Private VoidBtnopenie_click (ObjectSender, eventargs e ){// Start the IE ProcessProcess. Start ("Iexplore.exe");}
(2) Open the specified URL
Private Void Btnopenurl_click ( Object Sender, eventargs e ){// Method 1 // Start the IE process with Parameters Process. Start (" Iexplore.exe "," Http://www.cnblogs.com/SkySoot/ "); // Method 2 // Define a processstartinfo instance Processstartinfo startinfo = New Processstartinfo (" Iexplore.exe "); // Set Process Parameters Startinfo. Arguments =" Http://www.cnblogs.com/SkySoot/ ";// Minimize the process interface Startinfo. windowstyle = processwindowstyle. minimized; // Start the process Process. Start (startinfo );}
(2) open a folder
Private VoidBtnopenfolder_click (ObjectSender, eventargs e ){// Obtain the path of the "favorites" FileStringMyfavoritespath = system. environment. getfolderpath (environment. specialfolder. favorites );// Start the processSystem. Diagnostics. process. Start (myfavoritespath );}
(2) print documents
Private Void Btnprintdoc_click ( Object Sender, eventargs e ){ // Define a process instance Process myprocess = New Process (); Try { // Set Process Parameters String Mydocumentspath = environment. getfolderpath (environment. specialfolder. Personal); myprocess. startinfo. filename = mydocumentspath +" \ Txtfortest.txt "; Myprocess. startinfo. verb =" Print ";// Display all the predicates of the TXT file: open, print, printto Foreach ( String V In Myprocess. startinfo. verbs) {MessageBox. Show (V );} // Whether to start the process in a new window Myprocess. startinfo. createnowindow = True ; // Start the process Myprocess. Start ();} Catch (Win32exception ex ){ If (EX. nativeerrorcode = 1) {MessageBox. Show (ex. Message +" Check the path. "+ Myprocess. startinfo. filename );} Else If (EX. nativeerrorcode = 2) {MessageBox. Show (ex. Message +" You do not have permission to print this file. ");}}