簡體   English   中英

如何使用C ++在%APPDATA%中創建一個新文件夾?

[英]How to create a new folder in %APPDATA% using C++?

我試圖包括IOUtils庫並使用CSIDL命令,但是它不起作用...

這是執行此操作的代碼部分:

//------------------- 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 ------------------------------------

希望您能幫助我...非常感謝XD

您不應該將“ CSIDL_APPDATA”本身直接硬編碼到目錄路徑字符串中。 CSIDL_APPDATA是您必須在運行時使用Win32 API動態解析的虛擬文件夾的ID號(具體為26),例如:

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%的值,而不是使用CSIDLKNOWNFOLDERID

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);
}

我認為這應該可以幫助您(=。

文件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;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM