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. Use of file paths containing SMB host names
QtWS25 Last Chance

Use of file paths containing SMB host names

Scheduled Pinned Locked Moved General and Desktop
5 Posts 2 Posters 1.8k 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.
  • E Offline
    E Offline
    eos pengwern
    wrote on last edited by
    #1

    I'd like to be able to work with file and directory paths that include the name of the host computer in SMB format, for example:

    @//MY_OFFICE_PC/ProgramData/MyCompanyName/MyApplication/MyData.dat@

    I'm taking for granted of course that the user account which I'm logged into has the appropriate permissions to access the specified host. Here on my office network for example, I can open a console window and create and destroy files and directories on a couple of different computers in my local network, without any difficulty.

    However, I haven't found a way to be able to do this from within a Qt application. QDir, QFile etc. are all perfectly happy with paths of the form

    @C:/ProgramData/MyCompanyName/MyApplication/MyData.dat@

    but will not take

    @//MY_OFFICE_PC/ProgramData/MyCompanyName/MyApplication/MyData.dat@

    even when the application is running on MY_OFFICE_PC.

    My first thought was to try QUrl::toLocalFile, but this got me nowhere. In the code:

    @
    QString thePath = "//MY_OFFICE_PC/ProgramData/MyCompanyName/MyApplication/MyData.dat";
    QString theFullUrl = "file:" % thePath;
    QUrl theUrlObject(theFullUrl);
    QString theLocalPath = theUrlObject.toLocalFile();
    @

    ...the value of theLocalPath is "//MY_OFFICE_PC/ProgramData/MyCompanyName/MyApplication/MyData.dat", which is not especially helpful.

    The only other thing I can think of at the moment is to use QHostInfo to find the name of the computer on which my application is running, then to use a regexp to replace "//MY_OFFICE_PC/" by "C:/" and so at least obtain a solution for the case when the SMB path is in fact local. However, this doesn't help at all when I am working with a path to a file on another network computer.

    Is there a correct way to do this?

    1 Reply Last reply
    0
    • Chris KawaC Online
      Chris KawaC Online
      Chris Kawa
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Your examples contains "C:/" so I assume you're interested in Windows.

      Have you tried "\\MY_OFFICE_PC... ? At least for the protocol part. I don't think it matters for the following directory separators.

      1 Reply Last reply
      0
      • E Offline
        E Offline
        eos pengwern
        wrote on last edited by
        #3

        I hadn't tried that, but I tried it just now and I'm afraid it made no difference.

        1 Reply Last reply
        0
        • Chris KawaC Online
          Chris KawaC Online
          Chris Kawa
          Lifetime Qt Champion
          wrote on last edited by
          #4

          What if you replaced the share name with explicit ip address e.g."\\192.168.0.10... ? Does that work? If yes then the problem is transformed to resolving these ips.

          1 Reply Last reply
          0
          • E Offline
            E Offline
            eos pengwern
            wrote on last edited by
            #5

            I think I've found the solution.

            While experimenting with this, I realised that the directories I had been accessing from the console window weren't actually the same ones that I was trying to use from within Qt. In particular, the directories I was trying to access from Qt hadn't been explicitly shared.

            It turns out that (at least in Windows,) to address a directory using SMB, it is necessary for that directory to have been designated as shared even if it is a local directory on the same computer as the application is running on.

            So long as I take care of this, then the

            @//MY_OFFICE_PC/ProgramData/MyCompanyName/MyApplication/MyData.dat@

            syntax works just fine as-is, without having to invoke QUrl or do anything else; QFile and QDir understand this format quite happily.

            1 Reply Last reply
            0

            • Login

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