How to create a folder and write a file in it, using the UNC Path
-
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.
-
Hi
If on the windows platform thenQFile 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.
-
@chaithubk
Its possible it works too on windows, but i have not tried that.
I assume you mean like
QDir().mkdir("//ip/folder/newFolder"); -
@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 ?
-
@chaithubk
While you can detect network interfaces status in Qt
( QList<QNetworkInterface> ifaces = QNetworkInterface::allInterfaces(); )
http://doc.qt.io/qt-5/qnetworkinterface.htmlI 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. -
@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.
-
@chaithubk
No, I don't think so. If you are sayingQStorageInfo
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'sQStorageInfo()
uses in its underlying code.