Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to create a folder and write a file in it, using the UNC Path
QtWS25 Last Chance

How to create a folder and write a file in it, using the UNC Path

Scheduled Pinned Locked Moved Solved General and Desktop
qtcorenetwork share
9 Posts 3 Posters 4.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • chaithubkC Offline
    chaithubkC Offline
    chaithubk
    wrote on last edited by chaithubk
    #1

    I am writing a Qt application that receives the network shared path as a argument, the application is responsible to write a file to the shared network location. I am passing the argument filePath as "//<ip address>/<folder name>" and with this below code I am writing the file to the location:

    QStorageInfo storage(filePath);
    if (storage.isValid()) {
          // Do something 
    }
    else {
          // handle invalid path 
    }
    

    My code always hits the else block and not writing to the network shared location. If the same network path is mounted on the PC as say Z: drive and pass the filePath as "Z:\\", the file is written to the network location.

    Is there a way that I can pass the UNC path as an argument with out any need to physically map the network location to a drive and use it in my code.

    Thanks for the help.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      If on the windows platform then

      QFile file("//ip/folder/file.txt");
      file.open(QIODevice::WriteOnly);
      file.write("hello");
      file.close();
      

      Should work.
      As far as i know QStorageInfo works with mounted "paths" and cannot use UNC directly.

      So also depends on platform if u can use UNC directly.

      chaithubkC 1 Reply Last reply
      3
      • mrjjM mrjj

        Hi
        If on the windows platform then

        QFile file("//ip/folder/file.txt");
        file.open(QIODevice::WriteOnly);
        file.write("hello");
        file.close();
        

        Should work.
        As far as i know QStorageInfo works with mounted "paths" and cannot use UNC directly.

        So also depends on platform if u can use UNC directly.

        chaithubkC Offline
        chaithubkC Offline
        chaithubk
        wrote on last edited by chaithubk
        #3

        @mrjj can I create a directory too using QDir or something ?

        mrjjM 1 Reply Last reply
        0
        • chaithubkC chaithubk

          @mrjj can I create a directory too using QDir or something ?

          mrjjM Offline
          mrjjM Offline
          mrjj
          Lifetime Qt Champion
          wrote on last edited by
          #4

          @chaithubk
          Its possible it works too on windows, but i have not tried that.
          I assume you mean like
          QDir().mkdir("//ip/folder/newFolder");

          chaithubkC 1 Reply Last reply
          0
          • mrjjM mrjj

            @chaithubk
            Its possible it works too on windows, but i have not tried that.
            I assume you mean like
            QDir().mkdir("//ip/folder/newFolder");

            chaithubkC Offline
            chaithubkC Offline
            chaithubk
            wrote on last edited by chaithubk
            #5

            @mrjj Thank you, I will check that. Basically it's a NAS drive to which my application is trying to write the content. I want my application to continuously check if the network storage location is accessible all the time, if there is a network connection loss, then I need my application to detect that and resume the write once the connection is restored.

            Does Qt support such kind of handling or do I need to rely on integrating a 3rd party library like libcurl with Qt ?

            mrjjM 1 Reply Last reply
            0
            • chaithubkC chaithubk

              @mrjj Thank you, I will check that. Basically it's a NAS drive to which my application is trying to write the content. I want my application to continuously check if the network storage location is accessible all the time, if there is a network connection loss, then I need my application to detect that and resume the write once the connection is restored.

              Does Qt support such kind of handling or do I need to rely on integrating a 3rd party library like libcurl with Qt ?

              mrjjM Offline
              mrjjM Offline
              mrjj
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @chaithubk
              While you can detect network interfaces status in Qt
              ( QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); )
              http://doc.qt.io/qt-5/qnetworkinterface.html

              I am wondering if you could not just try to write to the nas box from time to time.
              if write fails, its not available and you schedule a test to try write again later.

              chaithubkC 1 Reply Last reply
              2
              • mrjjM mrjj

                @chaithubk
                While you can detect network interfaces status in Qt
                ( QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); )
                http://doc.qt.io/qt-5/qnetworkinterface.html

                I am wondering if you could not just try to write to the nas box from time to time.
                if write fails, its not available and you schedule a test to try write again later.

                chaithubkC Offline
                chaithubkC Offline
                chaithubk
                wrote on last edited by
                #7

                @mrjj yeah I was able to write to the UNC path. But I was using QStorageInfo in order to know the available disk space on the remote machine as you said, it looks like QStorageInfo is not working on the UNC path so is there a way that I can use a class like this to find the available size on the remote machine via UNC path.

                JonBJ 1 Reply Last reply
                0
                • chaithubkC chaithubk

                  @mrjj yeah I was able to write to the UNC path. But I was using QStorageInfo in order to know the available disk space on the remote machine as you said, it looks like QStorageInfo is not working on the UNC path so is there a way that I can use a class like this to find the available size on the remote machine via UNC path.

                  JonBJ Offline
                  JonBJ Offline
                  JonB
                  wrote on last edited by JonB
                  #8

                  @chaithubk
                  No, I don't think so. If you are saying QStorageInfo does not give the correct available disk space on a UNC, then that would imply that information is simply not available on a UNC/remote machine?

                  If you try to do this operation on the UNC via, say, Windows Explorer, does it let you see the available disk space, in the same way it does on a local drive?

                  P.S.
                  Have a read through https://www.codeproject.com/Questions/416466/How-to-find-available-space-on-a-UNC-path-serverNa and https://stackoverflow.com/questions/2050343/programmatically-determining-space-available-from-unc-path.

                  Either it's saying you have to use WMI, or maybe it's saying you can use GetFreeDiskSpaceEx(), or perhaps you have to map the UNC to a drive letter. I don't know what method Qt's QStorageInfo() uses in its underlying code.

                  chaithubkC 1 Reply Last reply
                  0
                  • JonBJ JonB

                    @chaithubk
                    No, I don't think so. If you are saying QStorageInfo does not give the correct available disk space on a UNC, then that would imply that information is simply not available on a UNC/remote machine?

                    If you try to do this operation on the UNC via, say, Windows Explorer, does it let you see the available disk space, in the same way it does on a local drive?

                    P.S.
                    Have a read through https://www.codeproject.com/Questions/416466/How-to-find-available-space-on-a-UNC-path-serverNa and https://stackoverflow.com/questions/2050343/programmatically-determining-space-available-from-unc-path.

                    Either it's saying you have to use WMI, or maybe it's saying you can use GetFreeDiskSpaceEx(), or perhaps you have to map the UNC to a drive letter. I don't know what method Qt's QStorageInfo() uses in its underlying code.

                    chaithubkC Offline
                    chaithubkC Offline
                    chaithubk
                    wrote on last edited by
                    #9

                    @JonB Yeah I had an issue with QStorageInfo() so I am using GetFreeDiskSpaceEx() to calculate the available space.

                    1 Reply Last reply
                    1

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved