简体   繁体   English

使用system()函数在C ++程序中运行cmd命令

[英]Running a cmd command within a C++ program using system() function

I want to move a file from its directory to C:\\Program files\\ so I ran this line of code 我想将文件从其目录移动到C:\\ Program files \\,所以我运行了这一行代码

system("copy /y \"%~dp0\\myfile\" \"C:\\Program files\"");

When compiling and executing this line of code and running it as an admin, it returns an error saying "The system cannot find the path specified." 当编译并执行此行代码并以管理员身份运行时,它将返回错误消息“系统找不到指定的路径”。 Even though, it runs fine as a .bat script. 即使这样,它也可以作为.bat脚本正常运行。 What's the problem ?! 有什么问题 ?!

Try using environment variables like this: 尝试使用如下环境变量:

system("copy /y \"%~dp0\\myfile\" \"%ProgramFiles%\"");

Or for access 32 bits Program Files folder use: 或访问32位Program Files文件夹,请使用:

system("copy /y \"%~dp0\\myfile\" \"%ProgramFiles(x86)%\"");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM