Use of file paths containing SMB host names

  • 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:


    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


    but will not take


    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?

  • Moderators

    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.

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

  • Moderators

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

  • 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


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

Log in to reply