no name no name - 1 month ago 19
C++ Question

How to write to text file on windows C dirve using Qt

I want to write a file on C drive on windows using Qt c++
what I try

QFile file("C:/key.txt");
if (!file.exists())
{
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.write("0");
file.close();
}


but nothing was written on C i think i should give it admin permissions so it can write to C drive but i search and found QFile::setpermissions
but it did't solve the problem too

so any idea to solve my problem please
Thanks in Avdance

Answer

You need to run your application with administrator privileges to write to C:. It requires to by pass UAC. To do that, embed the manifest:

QMAKE_LFLAGS += /MANIFESTUAC:"level='requireAdministrator' uiAccess='false'"

or you can adjust your application manifest:

<trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
  <security>
    <requestedPrivileges>
      <requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>
    </requestedPrivileges>
  </security>
</trustInfo>