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
Forum Update on Monday, May 27th 2025

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.
  • C Offline
    C Offline
    chaithubk
    wrote on 8 Nov 2018, 14:29 last edited by chaithubk 11 Aug 2018, 14:31
    #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
    • M Offline
      M Offline
      mrjj
      Lifetime Qt Champion
      wrote on 8 Nov 2018, 14:44 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.

      C 1 Reply Last reply 8 Nov 2018, 14:46
      3
      • M mrjj
        8 Nov 2018, 14:44

        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.

        C Offline
        C Offline
        chaithubk
        wrote on 8 Nov 2018, 14:46 last edited by chaithubk 11 Aug 2018, 14:49
        #3

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

        M 1 Reply Last reply 8 Nov 2018, 14:47
        0
        • C chaithubk
          8 Nov 2018, 14:46

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

          M Offline
          M Offline
          mrjj
          Lifetime Qt Champion
          wrote on 8 Nov 2018, 14:47 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");

          C 1 Reply Last reply 8 Nov 2018, 14:57
          0
          • M mrjj
            8 Nov 2018, 14:47

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

            C Offline
            C Offline
            chaithubk
            wrote on 8 Nov 2018, 14:57 last edited by chaithubk 11 Aug 2018, 14:57
            #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 ?

            M 1 Reply Last reply 8 Nov 2018, 15:11
            0
            • C chaithubk
              8 Nov 2018, 14:57

              @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 ?

              M Offline
              M Offline
              mrjj
              Lifetime Qt Champion
              wrote on 8 Nov 2018, 15:11 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.

              C 1 Reply Last reply 9 Nov 2018, 08:10
              2
              • M mrjj
                8 Nov 2018, 15:11

                @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.

                C Offline
                C Offline
                chaithubk
                wrote on 9 Nov 2018, 08:10 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.

                J 1 Reply Last reply 9 Nov 2018, 08:32
                0
                • C chaithubk
                  9 Nov 2018, 08:10

                  @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.

                  J Offline
                  J Offline
                  JonB
                  wrote on 9 Nov 2018, 08:32 last edited by JonB 11 Sept 2018, 08:49
                  #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.

                  C 1 Reply Last reply 9 Nov 2018, 17:14
                  0
                  • J JonB
                    9 Nov 2018, 08:32

                    @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.

                    C Offline
                    C Offline
                    chaithubk
                    wrote on 9 Nov 2018, 17:14 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

                    9/9

                    9 Nov 2018, 17:14

                    • Login

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