How to launch Windows applications (Notepad) / Call Batch files from a C#/.NET console application.
来源:互联网 发布:电玩巴士淘宝 编辑:程序博客网 时间:2024/05/29 07:00
- Download CallBatchFile project - 4.73 Kb
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 1
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
Process
class:Collapse |Copy Codeusing System.Diagnostics;
- Step 3
Add the following code for the
Main
function:Collapse |Copy CodeProcess 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
targetDir
is a local variable which stores the directory path for the batch file to execute.ProcessStartInfo
class (p.StartInfo
) hasworkingDirectory
,FileName
,Arguments
, andCreateNoWindow
properties for the new process.Start()
function will start the process.WaitForExit
function will make sure that process will wait till it finishes its processing. - Step 4
The whole program will be like this:
Collapse |Copy Codeusing 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 patchc:\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 offEcho Hello word ! From %1Pause
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 printHello 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:
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 to launch Windows applications (Notepad) / Call Batch files from a C#/.NET console application.
- How to launch App Store from my iPhone application?
- How to call an Objective-C Method from a C Method?
- Applications are expected to have a root view controller at the end of application launch
- Applications are expected to have a root view controller at the end of application launch
- Applications are expected to have a root view controller at the end of application launch
- Applications are expected to have a root view controller at the end of application launch
- in salesforce how to run batch from develop console and schedule job apex job
- How to launch application to start->run
- android - How to make a phone call from webview
- Windows Batch File: How to read a file
- How to register a keyboard shortcut (hot key) in a .NET Windows Forms Application
- Applications using Launch Screen Files and targetting iOS 7.1 and earlier need to also include a Lau
- How to call an external C function from within Oracle
- How to Call Java functions from C Using JNI
- How to Call Java Functions from C Using JNI
- How do I call a C++ function from C?
- How to import a batch of files into vob in ClearCase
- Linux - 标准输入转换为标准输出 代码(C)
- 中国人出境旅游每秒花3万元
- spring的最重要思想是什么?
- 我与C++设计模式(十七)——中介者模式
- Leetcode Symmetric Tree
- How to launch Windows applications (Notepad) / Call Batch files from a C#/.NET console application.
- linux系统编程-学习笔记5-opendir/readdir/getcwd
- Median of two sorted array
- linux下一般程序运行时查找动态库的顺序
- 全新版大学英语四综合教程答案
- B-树和B+树的应用:数据搜索和数据库索引
- 二叉树 前序,中序,后序遍历 --非递归遍历
- day42,page70
- [Unity3D]Unity3D游戏开发之Lua与游戏的不解之缘(上)