[import] How can I run another application or batch file from my Visual C# .NET code?
5293 단어 application
Suppose you want to run a command line application, open up another Windows program, or even bring up the default web browser or email program... how can you do this from your C# code?
The answer for all of these examples is the same, you can use the classes and methods in System.Diagnostics.Process to accomplish these tasks and more.
Example 1. Running a command line application, without concern for the results:
private void simpleRun_Click(object sender, System.EventArgs e){
System.Diagnostics.Process.Start(@"C:\listfiles.bat");
}
Example 2. Retrieving the results and waiting until the process stops (running the process synchronously):private void runSyncAndGetResults_Click(object sender, System.EventArgs e){
System.Diagnostics.ProcessStartInfo psi =
new System.Diagnostics.ProcessStartInfo(@"C:\listfiles.bat");
psi.RedirectStandardOutput = true;
psi.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;
psi.UseShellExecute = false;
System.Diagnostics.Process listFiles;
listFiles = System.Diagnostics.Process.Start(psi);
System.IO.StreamReader myOutput = listFiles.StandardOutput;
listFiles.WaitForExit(2000);
if (listFiles.HasExited)
{
string output = myOutput.ReadToEnd();
this.processResults.Text = output;
}
}
Example 3. Displaying a URL using the default browser on the user's machine:private void launchURL_Click(object sender, System.EventArgs e){
string targetURL = @http://woohoowoo.Duncan Mackenzie.net;
System.Diagnostics.Process.Start(targetURL);
}
In my opinion, you are much better off following the third example for URLs, as opposed to executing IE with the URL as an argument. The code shown for Example 3 will launch the user's default browser, which may or may not be IE; you are more likely to provide the user with the experience they want and you will be taking advantage of the browser that is most likely to have up-to-date connection information.C# code download available from http://woohoowoo.Duncan Mackenzie.net/samples/default.aspx
By the way, this post is a simple port of an earlier VB FAQ post ... just in case you thought you had seen it already in your feeds...
Article Source:
http://blogs.MSDN.com/super sharp FAQ/archive/2004/06/01/146375.aspx
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
Pre-Query SamplesValidate the current query criteria or provide additional query criteria programmatically, just before sending the SELEC...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.