C # common process operations

Source: Internet
Author: User
Public static void StartProgram (string directory, string fileName) {Process mainprocess = new Process (); mainprocess. startInfo. workingDirectory = directory; mainprocess. startInfo. fileName = fileName; // you do not need to specify the above directory. Here, you can write directory + filename as mainprocess. startInfo. arguments = "OK"; // You can pass the mainprocess parameter to other processes (exe files. startInfo. windowStyle = ProcessWindowStyle. normal; mainprocess. start () ;}// another process static Class Program {static void Main (string [] arguments) {string arg = arguments [0]; // arg = "OK ";}}

Start Process

// The process of killing cannot be followed by processname.exe. Remember !!! Public static void KillProcess (string processName) {Process myproc = new Process (); try {foreach (Process proc in Process. GetProcessesByName (processName) {if (! Proc. CloseMainWindow () {proc. Kill () ;}} catch (Exception ex) {throw ex ;}}

Kill Process

Private void PrintDoc () {// defines a process instance System. diagnostics. process myProcess = new System. diagnostics. process (); try {// set the Process parameter string myDocumentsPath = Environment. getFolderPath (Environment. specialFolder. personal); myProcess. startInfo. fileName = myDocumentsPath + "\ TxtForTest.txt"; myProcess. startInfo. verb = "Print"; // display all the predicates foreach (string v in myProcess. startInfo. verbs) MessageBox. show (v); myProcess. startInfo. createNoWindow = true; // start the process myProcess. start ();} catch (Win32Exception e) {if (e. nativeErrorCode = ERROR_FILE_NOT_FOUND) {MessageBox. show (e. message + "Check the path. "+ myProcess. startInfo. fileName);} else if (e. nativeErrorCode = ERROR_ACCESS_DENIED) {MessageBox. show (e. message + "You do not have permission to print this file. ");}}}

Print Document

// Start the browser: Process. start ("IExplore.exe"); // open the browser and jump to the specified URL: Process. start ("IExplore.exe", "http://www.cnblogs.com/ddhj/"); If Start is a folder path rather than a file path, this folder is opened: string myFavoritesPath = System. environment. getFolderPath (Environment. specialFolder. favorites); // obtain the "Favorites" file path // start the process System. diagnostics. process. start (myFavoritesPath );

Open the folder:

System. Diagnostics. Process. Start (FilePath );

Open a file in the folder:

System. Diagnostics. Process. Start (FilePath + "/" + FileName );

Open the folder and select a single file:

System. Diagnostics. Process. Start ("Explorer", "/select," + FilePath + "\" + FileName );

Or

System. Diagnostics. Process. Start ("assumer.exe", "/select," + FilePath + "\" + FileName );

Open the file with IE:

System. Diagnostics. Process. Start ("Explorer", FilePath + "\" + FileName );

System. Diagnostics. Process. Start ("assumer.exe", FilePath + "\" + FileName );

Note: (explorer,explorer.exe, select, case-insensitive, "/selecet," where "/," cannot be small, FilePath is the file path does not contain the file name)

 Open the folder and select the webpage

System. Diagnostics. Process. Start ("assumer.exe", "http://www.sunwale.com/");

 

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.