C++ 如何使用C+;在%APPDATA%中创建新文件夹+;?
我试图包含IOUtils库并使用CSIDL命令,但它不起作用 下面是代码中执行此操作的部分:C++ 如何使用C+;在%APPDATA%中创建新文件夹+;?,c++,directory,firemonkey,appdata,c++builder-xe6,C++,Directory,Firemonkey,Appdata,C++builder Xe6,我试图包含IOUtils库并使用CSIDL命令,但它不起作用 下面是代码中执行此操作的部分: //------------------- Includes ----------------------- #include <fmx.h> #include <IOUtils.hpp> #pragma hdrstop #include "Unit1.h" #include "Unit2.h" #include "Unit3.h" //--------------------
//------------------- Includes -----------------------
#include <fmx.h>
#include <IOUtils.hpp>
#pragma hdrstop
#include "Unit1.h"
#include "Unit2.h"
#include "Unit3.h"
//---------------------- end ------------------------
//---- On Form Show (bugged event: It doesn't create the needed folder) ----
void __fastcall TfrmInicio::FormShow(TObject *Sender)
{
if (TDirectory::Exists("CSIDL_APPDATA\\Nintersoft\\Ninterfin")) {
if (FileExists("CSIDL_APPDATA\\Nintersoft\\Ninterfin\\Inf.nf")) {
mmInfo->Lines->LoadFromFile("CSIDL_APPDATA\\Nintersoft\\Ninterfin\\Inf.nf");
}
}
else {
TDirectory::CreateDirectory("CSIDL_APPDATA\\Nintersoft\\Ninterfin");
}
}
//--------------------------------- end ------------------------------------
/--------------包括-----------------------
#包括
#包括
#布拉格语hdrstop
#包括“Unit1.h”
#包括“Unit2.h”
#包括“Unit3.h”
//----------------------结束------------------------
//----表单显示时(错误事件:它不会创建所需的文件夹)----
void\uu fastcall TfrmInicio::FormShow(TObject*发送方)
{
如果(TDirectory::Exists(“CSIDL\u APPDATA\\Nintersoft\\Ninterfin”)){
如果(文件存在(“CSIDL\u APPDATA\\Nintersoft\\Ninterfin\\Inf.nf”)){
mmInfo->line->LoadFromFile(“CSIDL_APPDATA\\Nintersoft\\Ninterfin\\Inf.nf”);
}
}
否则{
TDirectory::CreateDirectory(“CSIDL_APPDATA\\Nintersoft\\Ninterfin”);
}
}
//---------------------------------结束------------------------------------
我希望你能帮助我。。。
非常感谢XD我想这应该对你有所帮助 文件NewDirectory.cpp
//includes standard libraries
#include <iostream>
//includes windows libraries
#include <windows.h>
//includes header files
int NewDirectory(){
char *Directory = "C://Users/user/AppData/somefolder/";
//Checks if folder of file exist
int FilePath = PathFileExist(Directory);
//Makes new directory
int NewFolder = CreateDirectory(Directory, NULL);
if(!FilePath){
std::cout << "Error could not find folder, trying to create new Directory" << std::endl;
NewFolder;
if(!NewFolder){
std::cout << "Could not make new directory closing" << std::endl;
return 1;
} else {
std::cout << "New Directory" << Directory << "Created!" << std::endl;
return 0;
}
} else {
std::cout << "the Directory" << Directory << "already exist" << std::endl;
return 0;
}
}
//包括标准库
#包括
//包括windows库
#包括
//包括头文件
int NewDirectory(){
char*Directory=“C://Users/user/AppData/somefolder/”;
//检查文件的文件夹是否存在
int FilePath=PathFileExist(目录);
//创建新目录
int NewFolder=CreateDirectory(目录,NULL);
如果(!FilePath){
std::cout您不应该将“CSIDL\u APPDATA”本身直接硬编码到目录路径字符串中。CSIDL\u APPDATA
是一个虚拟文件夹的ID号(特别是26),您必须在运行时使用Win32 API动态解析该虚拟文件夹,例如:
void __fastcall TfrmInicio::FormShow(TObject *Sender)
{
WCHAR szPath[MAX_PATH+1] = {0};
if (SUCCEEDED(SHGetFolderPathW(FmxHandleToHWND(Handle), CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, szPath)))
{
String DirPath = TPath::Combine(szPath, L"Nintersoft\\Ninterfin");
TDirectory::CreateDirectory(DirPath);
String FileName = TPath::Combine(DirPath, L"Inf.nf");
if (TFile::Exists(FileName))
mmInfo->Lines->LoadFromFile(FileName);
}
}
或者,仅在Vista和更高版本上,使用SHGetKnownFolderPath()
:
void __fastcall TfrmInicio::FormShow(TObject *Sender)
{
PWSTR pszPath;
if (SUCCEEDED(SHGetKnownFolderPath(FOLDERID_RoamingAppData, 0, NULL, &pszPath)))
{
String DirPath = TPath::Combine(pszPath, L"Nintersoft\\Ninterfin");
CoTaskMemFree(pszPath);
TDirectory::CreateDirectory(DirPath);
String FileName = TPath::Combine(DirPath, L"Inf.nf");
if (TFile::Exists(FileName))
mmInfo->Lines->LoadFromFile(FileName);
}
}
或者,使用Sysutils::GetEnvironmentVariable()
检索%APPDATA%
的值,而不是使用CSIDL
或KNOWNFOLDERID
:
void __fastcall TfrmInicio::FormShow(TObject *Sender)
{
String DirPath = TPath::Combine(Sysutils::GetEnvironmentVariable(L"APPDATA"), L"Nintersoft\\Ninterfin");
TDirectory::CreateDirectory(DirPath);
String FileName = TPath::Combine(DirPath, L"Inf.nf");
if (TFile::Exists(FileName))
mmInfo->Lines->LoadFromFile(FileName);
}
您遇到了什么样的错误?它只是没有创建指定的文件夹…当然没有,因为您没有试图要求Windows将CSIDL\u APPDATA
转换为实际路径。哦,我现在很困惑…我以前问过,如何在另一个主题中获取%APPDATA%目录,其他用户说我可以使用CSIDL\u APPDATA或CSIDL_LOCAL_APPDATA作为路径根…其他用户所说的是正确的,但您误读了他的答案。他没有告诉您直接将“CSIDL…”
放入路径字符串中。他告诉您将所需的CSIDL
ID号传递给SHGetSpecialFolderPath()
。阅读我的答案,它向您展示如何使用CSIDL
方法,尽管使用SHGetFolderPath()
代替(它取代了SHGetSpecialFolderPath()
。请记住CreateDirectory()
失败,如果目录已经存在,则会出现一个错误。\u已经存在
错误。您的示例不处理这种情况。他特别希望使用%APPDATA%的解析。
此代码不符合要求。我不想告诉您此代码符合要求,它会在%APPDATA%目录中创建一个文件夹,所以请确保您知道w你在暗示之前所说的是错误的。如果目录已经存在,@Remy lebeau我的代码会处理它,如果目录已经存在,它会输出目录已经存在。@Daniel:此代码不符合用户的要求,因为你硬编码了整个路径,而不是使用%APPDATA%
或CSIDL\u APPDATA
。不同系统上的实际路径可能不同(C:\Users\\APPDATA
不是Win7之前系统上的默认路径),这就是为什么存在%APPDATA%
和CSIDL_APPDATA
的原因。你知道有多少人的用户名是user
?这段代码需要一个特定的库还是只需要winbase.h就可以了?抱歉,我学习得很慢,我还是C++SHGetFolderPath()和shGetKnowlderPath()的新手
都位于Shell32.dll
和GetEnvironmentVariable()中在RTL本身,并将Win32版本封装在代码> Kerner32.DLL< /Calp>中。RTL已经为您链接到两个库。您好,我在这里只是说,我已经更改了最新的代码,以使我的程序工作得很好。前两个选项我不知道为什么它不能工作。C++Builder说:未知的命令,这个问题发生在:SHGetKnownFolderPath;FOLDERID_RoamingAppData;CSIDL_APPDATA和SHGetFolderPathW。感谢您的支持!XD您需要在代码中添加#include
,才能使用这些函数。啊,好的,当我问是否需要添加其他库时,我是这么说的……所以,谢谢。