How to launch Windows applications (Notepad) / Call Batch files from a C#/.NET console application.
2014-10-06 09:57
661 查看
Download CallBatchFile project - 4.73 Kb
Demonstration of passing parameters to the batch file and accessing passed parameters in a batch file
Create a Console project called as CallBatchFile.
Your directory structure created would be like this after clicking the OK button:
Step 2
Include the following namespace for the
Collapse |
Copy Code
Step 3
Add the following code for the
Collapse |
Copy Code
Step 4
The whole program will be like this:
Collapse |
Copy Code
Step 5
Configuration: Create a new directory called as BatchFile to the patch
c:\temp\CallBatchFile. Your new directory structure would be like below:
Step 6
Create a new batch file called as MyBatchFile.bat and add the following lines:
Collapse |
Copy Code
First line is for Echo off which will make sure that the batch file will not print each line on the screen while file is executing. Second line will print
Hello world ! with %1 (the parameter passed to the batch file). Third line is to pause the execution of the batch file.
Step 7
Compile the program and execute it. You should see the following result:
There are two windows created: one for the console application called CallBatchFile.exe and another for the new process where we are trying to execute the batch file.
Note: Remember we have earlier used
to continue the execution.
Collapse |
Copy Code
This will open Notepad.
Introduction
This article is targeted for mid-level programmers who have already worked on C# language. This article uses batch files for demonstration and the reader should have preliminary knowledge of batch files.Technology Used
Sample application describes how to call any Windows application or batch file using a C# console application. This application launches a new process and executes Windows applications in that process.Demonstration of passing parameters to the batch file and accessing passed parameters in a batch file
API Used
System.Console.WriteLine()
System.Diagnostics.Process
Process.Start()
Using the code
Step by Step process to create a sample application
Step 1Create a Console project called as CallBatchFile.
Your directory structure created would be like this after clicking the OK button:
Step 2
Include the following namespace for the
Processclass:
Collapse |
Copy Code
using System.Diagnostics;
Step 3
Add the following code for the
Mainfunction:
Collapse |
Copy Code
Process p=null; try { string targetDir; targetDir = string.Format(@"C:\Temp\CallBatchFile\BatchFile"); p= new Process(); p.StartInfo.WorkingDirectory = targetDir; p.StartInfo.FileName = "MyBatchFile.bat"; p.StartInfo.Arguments = string.Format("C-Sharp Console application"); p.StartInfo.CreateNoWindow = false; p.Start(); p.WaitForExit(); } catch (Exception ex) { Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString()); }
Explanation about code
targetDiris a local variable which stores the directory path for the batch file to execute.
ProcessStartInfoclass (
p.StartInfo) has
workingDirectory,
FileName,
Arguments, and
CreateNoWindowproperties for the new process.
Start()function will start the process.
WaitForExitfunction will make sure that process will wait till it finishes its processing.
Step 4
The whole program will be like this:
Collapse |
Copy Code
using System; using System.Diagnostics; namespace CallBatchFile { /// <summary> /// Summary description for Class1. /// </summary> class Class1 { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main(string[] args) { Process p=null; try { string targetDir; targetDir = string.Format(@"C:\Temp\CallBatchFile\BatchFile"); p= new Process(); p.StartInfo.WorkingDirectory = targetDir; p.StartInfo.FileName = "MyBatchFile.bat"; p.StartInfo.Arguments = string.Format("C-Sharp Console application"); p.StartInfo.CreateNoWindow = false; p.Start(); p.WaitForExit(); } catch (Exception ex) { Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString()); } } } }
Step 5
Configuration: Create a new directory called as BatchFile to the patch
c:\temp\CallBatchFile. Your new directory structure would be like below:
Step 6
Create a new batch file called as MyBatchFile.bat and add the following lines:
Collapse |
Copy Code
@echo off Echo Hello word ! From %1 Pause
First line is for Echo off which will make sure that the batch file will not print each line on the screen while file is executing. Second line will print
Hello world ! with %1 (the parameter passed to the batch file). Third line is to pause the execution of the batch file.
Step 7
Compile the program and execute it. You should see the following result:
There are two windows created: one for the console application called CallBatchFile.exe and another for the new process where we are trying to execute the batch file.
Note: Remember we have earlier used
p.StartInfo.CreateNoWindow=false, but still the new window is coming. This is because the batch file has pause command and it needs user's intervention
to continue the execution.
Calling Notepad using the same program
Change Step 3 code as mentioned below:Collapse |
Copy Code
Process p=null; try { p= new Process(); p.StartInfo.FileName = "notepad"; p.Start(); p.WaitForExit(); } catch (Exception ex) { Console.WriteLine("Exception Occurred :{0},{1}", ex.Message,ex.StackTrace.ToString()); }
This will open Notepad.
相关文章推荐
- [导入]How can I run another application or batch file from my Visual C# .NET code?
- How to call Visual Basic .NET run-time library members from Visual C#
- How to call the EnumJobs function from a Visual Basic .NET application
- How to Share Session/Application State Across Different ASP.NET Web Applications
- How To Implement Forms-Based Authentication in Your ASP.NET Application by Using C# .NET
- How to use Console.WriteLine in ASP.Net (C#) during debug?
- How To Create an ASP.NET Application from Multiple Projects for Team Development
- Class To Change Wallpaper In Xp,Vista,Windows 7 From C#.Net
- How to call C/C++ sytle function from C# solution?
- How to automate Microsoft Excel from Microsoft Visual C#.NET
- How to make a call in Windows Phone 7 application
- How to programmatically generate C# files from a DLL or EXE
- How to launch App Store from my iPhone application?
- How to use Console.WriteLine in ASP.Net (C#) during debug?
- [C#/.NET]how to implement web application localization in .net 4.0
- How to use .NET Windows Control into the WPF Application
- How to register a keyboard shortcut (hot key) in a .NET Windows Forms Application
- How to set processor affinity from Batch File for Windows XP
- Summarize the steps how to create a .NET application[摘自Beginning Visual C# 2008]
- HOW TO: Set a Windows Hook in Visual C# .NET