0

我试图包含 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

4

2 回答 2

3

您不应该将“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%而不是使用CSIDLor 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);
}
于 2014-08-01T01:09:17.950 回答
-1

我认为这应该可以帮助您(=。

文件 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;
    }
}
于 2014-08-01T00:39:56.773 回答