user7179690
user7179690

Reputation: 1123

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

Upvotes: 0

Views: 519

Answers (2)

Nikita
Nikita

Reputation: 6427

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> 

Upvotes: 5

Hatted Rooster
Hatted Rooster

Reputation: 36483

Writing directly on the C: drive requires admin privileges. Either change the folder or start the application as admin.

Upvotes: 1

Related Questions