在程序中调用bat文件

1、C++调用dos命令

    在Microsoft Visual C++环境下编写c++程序可以包含头文件<stdlib.h> 然后在main函数体中调用system()函数,可以调用dos命令。

    例如:想要程序在某个地方停住使用system("PAUSE");就可以暂停;

    system("CLS");可以清屏;

    system("DIR C:");可以查询C盘;

    system("start regedit.exe");打开注册表;

    system("net user");查看本地用户组,等等……

 

2、在程序中如何调用执行bat文件或者exe文件

C#中

   1、执行批处理文件System.Diagnostics.Process.Start(path);   

   path为文件路径   

   2、修改批处理文件

   FileStream aFile = new FileStream(filePath, FileMode.OpenOrCreate);       

   temp = "";  //批处理文件中的内容   

   charData = temp.ToCharArray();   

   Encoder e = Encoding.UTF8.GetEncoder();    

   e.GetBytes(charData, 0, charData.Length, byData, 0, true);  //字符型数组转换成字节型数组   

   aFile.Write(byData, 0, byData.Length);   

   aFile.Close();   

 

C中

   system("*.bat"); 

   system("*.exe");

   #include <process.h>

   也不一定要在同一个目录下,只要想执行的.bat文件   

   1.在系统path变量的各目录下(在命令提示行执行path就知道了)   

   2.在程序所在目录   

   3.前两者都可以直接按楼上的方法,否则,就要指定绝对路径(e.g.: D:\prog\test.exe)或相对路径(e.g.: \sub\subsub\test2.exe)

     要注意的是:'\'需要用转义字符'\\'来表示(或者可以试一试“/”)。

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

这里以C为例,代码如下:

[cpp]  view plain  copy
  1. #include "stdio.h"  
  2. #include "stdlib.h"  
  3. #include <process.h>  
  4. void main()  
  5. {     
  6.     system("a.bat");  
  7.     //system("PAUSE");  
  8. }  

//a.bat的代码

[plain]  view plain  copy
  1. type *.txt > a  

在把文件a,修改后缀名为a.txt


或者直接使用b.bat的代码(b.bat用来改变文件a的后缀名)

[plain]  view plain  copy
  1. @echo off  
  2. ren a a.txt  

   综合起来,可以写成如下.bat代码:

[plain]  view plain  copy
  1. type *.txt > a  
  2. @echo off  
  3. ren a a.txt  


转:https://blog.csdn.net/sanqima/article/details/37764049

猜你喜欢

转载自blog.csdn.net/eric_e/article/details/80516028