C # Call Microsoft's built-in calculator, notepad, and other software,

Source: Internet
Author: User

C # Call Microsoft's built-in calculator, notepad, and other software,
Basic knowledge:

Process class
Provides access to local and remote processes and enables you to start and stop local system processes.
Namespace: System. Diagnostics
Assembly: System (in System. dll)
Syntax: public class Process: Component

Process. Start method (ProcessStartInfo)
Start the Process resource specified by the parameter that contains the Process startup information (for example, the name of the Process to be started) and associate the resource with the new Process component Syntax: public static Process Start (ProcessStartInfo startInfo)

The Process component provides access to processes running on the computer. In the simplest words, a process is the currently running application. A thread is the basic unit for the operating system to allocate processor time to it. Code of any part of a thread executable process, including the part currently executed by another thread.

The Process component is a useful tool for starting, stopping, controlling, and monitoring applications. You can use the Process component to obtain a list of running processes or start a new Process. The Process component is used to access system processes. After the Process component is initialized, you can use this component to obtain information about the currently running Process. This information includes the thread set, loaded modules (. dll and. exe files), and performance information (such as the amount of memory currently used by the process ).

If a PATH variable is declared with quotation marks in the system, the path must be fully qualified when any process in this position is started. Otherwise, the path cannot be found. For example, if c: \ mypath is not in your path and you add it with quotation marks (path = % path %; "c: \ mypath"), start c: you must limit any processes in \ mypath.

The process component also obtains information about a group of attributes. After the Process component obtains information about one member in any group, it caches the values of other attributes in the group and before you call the Refresh method, do not obtain new information about other members in the group. Therefore, it is not guaranteed that the attribute value is compared with the last call update of the Refresh method. Group segmentation is related to the operating system.

A system process is uniquely identified by its process identifier. Like many Windows resources, a process is identified by its handle, which may not be unique on a computer. A handle is a general term used to indicate a resource identifier. Even if the Process has exited, the operating system still keeps the Process Handle, which is accessed through the Handle attribute of the Process component. Therefore, you can obtain the management information of processes, such as ExitCode (usually, or zero indicates success, or non-zero error code) and ExitTime. Handle is a very valuable resource, So handle leakage is more harmful than memory leakage.

ProcessStartInfo class
Specifies a group of values used to start a process.
Syntax: public sealed class ProcessStartInfo

ProcessStartInfo. FileName attribute
Obtain or set the application or document to be started
Syntax: public string FileName {get; set ;}
ProcessStartInfo is used with the Process component. When using the Process class to start a Process, you can also access the Process information that is appended to the Process information available when the current running Process is running.

You can use the ProcessStartInfo class to better control the startup process. At least the FileName attribute must be set manually or using constructors. The file name is any application or document. Here, the document is defined as any file type associated with open or default operations. Use the "Folder Options" dialog box provided by the operating system to view the registered file types and related applications on the computer. Click the "advanced" button to open a dialog box that shows whether an open operation is associated with a specific registered file type.

You can also set other attributes that define the operations to be performed on the file. You can specify the type value specific to the FileName attribute for the Verb attribute. For example, you can specify "print" for the document type ". In addition, you can also specify the Arguments attribute value, which will be the command line parameter passed to the file opening process. For example, if you specify a text editor application in the FileName attribute, you can use the Arguments attribute to specify a text file that will be opened in the editor.

The standard input mode is usually the keyboard, and standard output and standard errors are usually displayed on the monitor screen. However, you can use the RedirectStandardInput, RedirectStandardOutput, and RedirectStandardError attributes to get input from a file or another device, or return the output to a file or another device. If you use the StandardInput, StandardOutput, or StandardError attributes on the Process component, you must first set the corresponding values on the ProcessStartInfo attribute. Otherwise, an exception occurs when reading or writing data from a stream.

Set UseShellExecute to specify whether to use the Operating System shell to start the process.
You can change the value of any ProcessStartInfo attribute until the process starts. After the process is started, changing these values does not work.

Practical application:

Create a new WPF Application and add a button in the. xaml File
Add a click event for a button
Add the following code to the. cs click function:

Private void button#click (object sender, RoutedEventArgs e) {System. diagnostics. processStartInfo Info = new System. diagnostics. processStartInfo (); Info. fileName = "calc.exe"; // "calc.exe" is the calculator, and "notepad.exe" is the notepad System. diagnostics. process Proc = System. diagnostics. process. start (Info );}

Running result:

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.