Comment créer un nouveau dossier dans %APPDATA% en utilisant C++ ?
-
26-12-2019 - |
Question
J'ai essayé d'inclure la bibliothèque IOUtils et d'utiliser la commande CSIDL, mais cela ne fonctionne pas...
Voici la partie du code qui le fait :
//------------------- 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 ------------------------------------
J'espère que vous pourrez m'aider...Merci beaucoup XD
La solution
Vous n'êtes pas censé coder en dur "CSIDL_APPDATA" directement dans la chaîne de chemin de votre répertoire. CSIDL_APPDATA
est un numéro d'identification (en particulier 26) pour un dossier virtuel que vous devez résoudre dynamiquement au moment de l'exécution à l'aide de l'API Win32, par exemple :
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);
}
}
Alternativement, sur Vista et versions ultérieures uniquement, utilisez SHGetKnownFolderPath()
plutôt:
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);
}
}
Alternativement, utilisez Sysutils::GetEnvironmentVariable()
pour récupérer la valeur de %APPDATA%
au lieu d'utiliser un CSIDL
ou 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);
}
Autres conseils
Je pense que cela devrait vous aider (=.
fichier nouveaudirectory.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;
}
}