如何在%appdata%中创建文件夹,在其中创建.bat文件,然后执行它?
How to create folder in %appdata%, create .bat file in it, and then execute it?
我正在创建C++代码,它将创建一些.bat
文件并将其存储在%appdata%
文件夹中。我已经成功创建了文件,但仍然无法创建文件夹并执行它。
下面是我的简单代码,它看起来并不简单,但它可以在%appdata%
中创建.bat
文件,也许有人可以帮助我找到简单的文件。
#include <iostream>
#include <stdio.h>
#include <fstream>
#include <sstream>
#include <string>
#include <windows.h>
#include <direct.h>
int main(int argc, char **argv) {
using namespace std;
std::ofstream aaa;
ostringstream aaa;
aaa.open(aaa1.str());
aaa1 << getenv("appdata") << "/"
<< "test.bat";
aaa.open(aaa1.str());
Updater << "@echo on" << endl;
Updater << "echo "on"" << endl;
return 0;
}
代码在%appdata%
中成功创建了.bat
文件,但我需要存储在%appdata%
的新文件夹中,比如New Folder
,然后执行.bat
文件。
创建目录
1st使用字符串中的 _dupenv_s() 获取路径添加新文件夹名称"\New Folder">
2nd使用 _mkdir(str.c_str()) 创建目录;3rd使用 std::ofstream outf(str) 创建"test.bat";
#include "stdafx.h"
#include<fstream>
#include<iostream>
#include<conio.h>
#include<direct.h>
using std::cout;
using std::cin;
using std::endl;
int tmain(int argc, TCHAR* argv[])
{
char *pValue;
size_t len;
errno_t err = _dupenv_s(&pValue, &len, "APPDATA");
std::string NewFile = "\new";
std::string str(pValue);
str = str + NewFile;
_mkdir(str.c_str());
str = str + "\Sample.bat"; //
std::ofstream outf(str);
if (!outf)
{
printf("error ");
}
outf << "this is line1" << endl;
outf << "line 2" << endl;
return 0;
}
请!如果有帮助,不要忘记投票
在用户可写位置创建/运行可执行文件是要小心的事情(利用人们运行提升的进程,然后运行攻击有效负载),否则只是将几件事联系在一起。
在Windows上,大多数这些环境变量都是出于旧版/兼容性原因而存在的,SHGetKnownFolderPath
是查找文件夹的现代方法。它为路径分配足够的空间,小心使用C-API的手动内存,尽快对其进行unique_ptr
或wstring
。它从Vista工作,如果真的需要,还有较旧的API。
wchar_t *str = nullptr;
SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, NULL, &str); // CHECK RETURN
...use str...
CoTaskMemFree(str);
还要注意文件路径中的 Unicode 和空格。
进程有两个选项,cstdlib
标头中有system(command_line)
,或者对于高级用途,请查看 WindowsCreateProcessW
.像这样:
STARTTUPINFO startup;
startup.cb = sizeof(startup);
PROCESS_INFORMATION pi;
CreateProcessW(NULL, L"cmd.exe /C C:\ThePath\myfile.bat", NULL, NULL, FALSE, 0, NULL, NULL, &startup, &pi);
显然特定于Windows。Linux,Mac等都有自己的文件系统布局和安全性。
C++fstream
不会自动为您创建目录。您可以将此类目录设置为安装程序的一部分,但是要在运行时执行此操作C++17 具有std::filesystem::create_directories
,它采用路径。如果无法使用 C++17,请使用CreateDirectory
或_mkdir
。同样在Windows上要注意Unicode。
- 我如何创建一个列表,然后从中创建两个列表,其中一个用于偶数,另一个用于奇数?
- 如何创建一个C++程序来读取字符串数组中的信息,然后将其排序到类中?
- 将 int 转换为字符串,然后连接另一个变量以创建完整扩展名,然后将其转换为 const_char*
- C++ 如何创建 2D 数组,将其传递给另一个函数,然后打印
- 在功能中创建一个数组,然后将其返回到main()
- C++ std::vector 创建对象然后添加对象与在向量中创建对象之间的区别?
- 如何在给定任意数量的整数的情况下创建一个唯一键?并使用该键存储,然后从地图中查找
- 在构造函数中创建一个字符串数组,然后在主函数中使用它
- 如何在%appdata%中创建文件夹,在其中创建.bat文件,然后执行它?
- 读取该文件,然后创建动态数组
- 创建一个 const unique_ptr然后尝试从它 std::move 会给出与您尝试访问复制构造函数相同的错误
- 如何在C++中创建一个网格,这将允许我将每个索引设置为一个向量,以便以后操作该向量,然后绘制它
- 如何预先分配空类对象,然后在if语句的大括号中创建该对象
- 如果没有数据,则创建一个包含数据的文本文件,然后读取它以检索数据
- 创建指向未符号char []的指针,然后将其传递给SF :: Soundbuffer :: LoadFromememor
- 使用 ros launch 创建一个目录,然后将其路径传递给不同的节点
- 如何在qthread上创建QTCPSERVER,然后从主线程中将其停止
- C++,如何创建和绘制二叉树,然后按预购方式遍历它
- 在函数内部创建一个局部向量,然后将其作为引用传递给其他函数,这些函数将其存储为引用
- 读取文件,然后创建数组函数C++