Qt copy files to C disk directory super administrator privileges and avoid VirtualStore function

I thought copying files was a very simple matter, but I didn't expect to need super administrator privileges.

There are many tutorials on the Internet, such as this one:

Summary of adding administrator privileges to Qt programs (there must be a way you haven’t seen ) , you will find that there is no problem with the code, but the creation fails. And when we right-click the program and run it with administrator privileges, the file can be created normally. At this point, it means that our program does not have administrator privileges by default, so some directories cannot be written. Depending on our compiler, there are two situations as follows. 1. Use the MSVC compiler to add the following line to the pro file: QMAKE_LFLAGS += /MANIFESTUAC:\"level=\'requireAdministrator\' uiAccess=\'false\'\" After recompilation, the program https://blog . csdn.net/zyhse/article/details/108166181 Then add the code to copy the file and it will be ok:

// 获取程序路径和需要复制的文件路径
QString programPath = QCoreApplication::applicationDirPath();
QString sourceFilePath = programPath+"/file.dat"; // 源文件路径
QString targetFilePath = "C:/file.dat"; // 目标文件路径

qDebug() << sourceFilePath;

QFile targetFile(targetFilePath);
if (!targetFile.exists()) {

    // 复制文件
    QFile sourceFile(sourceFilePath);
    if (sourceFile.exists()) {
        if (!QFile::copy(sourceFilePath, targetFilePath)) {
            qDebug() << "Failed to copy file.";
        }else
        {
            qDebug() << "Copy file success!";
        }
    } else {
        qDebug() << "Source file does not exist.";
    }

}else
{
    qDebug() << "File exist.";
}

The above method of obtaining administrator privileges is good, but it always feels a little troublesome.

You may not have heard of this VirtualStore feature :

VirtualStore is a virtual folder in the Windows operating system used to store changes made by users without administrator privileges in a protected directory. When a user tries to make changes in a protected directory, due to lack of administrator privileges, the operating system saves the changes to the VirtualStore folder instead of directly to the original directory.

VirtualStore is typically used to store configuration files for applications and user data. In Windows Vista and later operating systems, when the user makes changes in Program Files and folders under the Windows directory, the operating system saves those changes to the VirtualStore folder. This is because these directories are protected and only administrator privileges can change them.

Although VirtualStore can help users change protected directories without administrator rights, it can also cause some problems. For example, a user may forget about a change in the VirtualStore, causing the application to not work properly. Therefore, developers need to be aware of the existence of the VirtualStore and ensure that their applications can properly handle the data in the VirtualStore.

To solve this problem, developers can use the QStandardPaths class provided by Qt to obtain the standard paths of applications to ensure that they can work properly on any Windows operating system. The QStandardPaths class provides many static functions to obtain different types of standard paths, such as the application's configuration directory, data directory, cache directory, etc. These functions will automatically take into account the existence of the VirtualStore and return the correct path.

In addition to using the QStandardPaths class, developers can also avoid using the VirtualStore by using the program data directory instead of the installation directory in the application. The program data directory refers to the directory where data and configuration files are saved when the application is running. By keeping these files in the program data directory, applications can avoid using the VirtualStore and work correctly on different Windows operating systems.

Comparison chart:

qt official online help documentation: https://doc.qt.io/qt-5/qstandardpaths.html 

Guess you like

Origin blog.csdn.net/xuexixiaoshizhe/article/details/130805441