System.Diagnostics.Process.Start Usage in net

Source: Internet
Author: User
Tags documentation

System.Diagnostics.Process.Start (); This method is used to create a new process, for example, if you want to add a hyperlink to a menu on a program to Baidu, you can use this method to realize System.Diagnostics.Process.Start ("Iexplore.exe", "http://zhidao.baidu.com"); The first argument in front can be omitted but if you have an open page before then you will be able to jump from that page to http://zhidao.baidu.com open a new window without opening it. You can also use it to open a local program, For example, if you click a button to open the Registry Editor, it should be System.Diagnostics.Process.Start (@ "C:\WINDOWS\regedit.exe");

--------------------------------------------------------------------------------------------------------------- --------------------

problems encountered in the System.Diagnostics.Process class application in. NET the client program you developed needs to create a Word document from the Image field in the database, and then call the Word program to open the The user is edited and saved and then stored back into the database. The System.Diagnostics.Process class is used to invoke word to open the document, and the relevant code is as follows:
String TempPath = System.Environment.GetEnvironmentVariable ("TEMP");
String fileName = Path.Combine (TempPath, "01.doc");

Process wordprocess = new process ();
WordProcess.StartInfo.FileName = FileName;
WordProcess.StartInfo.Verb = "edit";
WordProcess.StartInfo.UseShellExecute = true;
Wordprocess.start ();
Wordprocess.waitforexit ();
Wordprocess.close ();

MessageBox.Show ("Word exited!");


The last few days have been useful, but today in the implementation to Wordprocess.waitforexit (); This is an unexpected sentence when the message is "no process associated with this object." A closer look at the relevant documentation in MSDN is not explained in much detail, but it is included in the documentation for the WaitForExit () method that mentions possible surprises. It suddenly occurred to me that there was no possibility that the word process was already open, so it was not associated with the current process. After you turn off the other word processes that are running, you will have no problem with the execution.

After some research, finally found that there is no instance of Word running in the case can achieve the requirements of the previous article, the code is as follows:

?

   string temppath = System.Environment.GetEnvironmentVariable ("TEMP");    string fileName = Path.Combine (TempPath, "Inference 01.doc");    string winwordpath = "";   //Determine if a Word instance is already running in the system.     process[] wordprocesses = Process.getprocessesbyname ("Winword");    foreach (Process process in wordprocesses) {        Debug.WriteLine ( Process. MainWindowTitle);         Winwordpath = process. mainmodule.filename;       ///if available, obtain the fully qualified name of the Winword.exe.        break;    }     Process wordprocess = new process ();    if (winwordpath.length > 0)    //If there is an instance of Word running, use the/w parameter to force a new instance to be started and pass the file name as a parameter. {        wordProcess.StartInfo.FileName = Winwordpath;          WordProCess. Startinfo.useshellexecute = false;         wordProcess.StartInfo.Arguments = fileName + "/w";    }    else                            //If no instance of Word is running, or {         wordProcess.StartInfo.FileName = FileName;         WordProcess.StartInfo.UseShellExecute = true;    }     Wordprocess.start ();     wordprocess.waitforexit ();       //The current process has been waiting until the Word instance exits.     Wordprocess.close ();

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.