[导入]How can I run another application or batch file from my Visual C# .NET code?
2004-09-22 09:33
579 查看
Posted by: Duncan Mackenzie, MSDN
This post applies to Visual C# .NET 2002/2003
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:
Example 2. Retrieving the results and waiting until the process stops (running the process synchronously):
Example 3. Displaying a URL using the default browser on the user's machine:
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://www.duncanmackenzie.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...
文章来源:http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx
This post applies to Visual C# .NET 2002/2003
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://www.duncanmackenzie.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://www.duncanmackenzie.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...
文章来源:http://blogs.msdn.com/csharpfaq/archive/2004/06/01/146375.aspx
相关文章推荐
- How can I run an EXE from within my application?
- How to launch Windows applications (Notepad) / Call Batch files from a C#/.NET console application.
- How to call Visual Basic .NET run-time library members from Visual C#
- How Can One Transfer Xcode 4 Code Snippets from One Machine to Another
- How to automate Microsoft Excel from Microsoft Visual C#.NET
- ubuntu 复制文件到xp (How i can copy file from server(OS:ubuntu 10.04) to my PC
- This code compacts and repairs an MS Access database from a C# .NET application
- How to write an application that supports the Fast User Switching feature by using Visual Basic .NET or Visual Basic
- How Can i Transfer Xcode 4 Code Snippets from One Machine to Another?
- how can i get the source code path && file names from an ELF file(compired with -g)?
- Summarize the steps how to create a .NET application[摘自Beginning Visual C# 2008]
- How to transfer data to an Excel workbook by using Visual C# 2005 or Visual C# .NET
- HOWTO:Turn Off PDA Display while your application is running.(VB.Net/c#+.Net CF+PPC2003) from CodeProject
- [导入]How can I update my user interface from a thread that did not create it?
- How can I play a wav-file from memory or BLOB-field?
- How can I recover a file that I deleted in my local repo from the remote repo in Git?
- How to call the EnumJobs function from a Visual Basic .NET application
- How Can I Launch The Appstore App Directly from my Application
- ASP.NET 调试出现<%@ Application Codebehind="Global.asax.cs" Inherits="XXX.XXX.Global" Language="C#" %>
- How do I execute or run an XML file?