可以用stdlib下的system函数,不需要using namespace std因为没有用到C++的特性
附代码:
#includeint main(){ system("C:\Users\Administrator\Desktop\a.txt"); return 0; }
还可以用ShellExecute,函数原型:
SHSTDAPI_(HINSTANCE) ShellExecuteA (HWND hwnd, LPCSTR lpOperation, LPCSTR lpFile, LPCSTR lpParameters, LPCSTR lpDirectory, INT nShowCmd);
其中hwnd指父窗口的句柄,可以设置为NULL
lpOperation指函数执行的指令,“open”是打开文件
IpFile指源文件路径
lpParameters指命令行参数,也可设置为NULL
lpDirectory是默认目录,也可为NULL
nShowCmd是指令,常用的指令有:
SW_HIDE 隐藏窗口
SW_SHOWNORMAL SW_NORMAL SW_SHOW SW_SHOWDEFAULT都差不多的 都是显示并让窗口进入活动状态
SW_MAXIMIZE 让窗口全屏
SW_MINIMIZE 让窗口最小化
附代码:
#includeint main(){ ShellExecute(NULL,"open","C:\Users\Administrator\Desktop\a.exe",NULL,NULL,SW_SHOW); }
如果想让文件以管理员身份运行,那可以用ShellExecuteEx
函数原型:SHSTDAPI_(WINBOOL) ShellExecuteEx (SHELLEXECUTEINFO *pExecInfo);
附代码:
void OpenExe(const char cc[],UINT ui){
int i=0,x=0;
for(const char *c;*c!=' ';c++){
if((*c)=='\') x=i;
i++;
}
char cc1[x+1];
for(i=0;i



