I'm working on a Java GUI application that can unmount and mount Samba shares to a specific letter drive. The drives are successfully unmounted and mounted when the user selects, however the drive label (the display name of drive) is not updating in windows file explorer.
I'm using the ProcessBuilder
to execute the command and I don't run into this issue when I just use the windows CMD myself but the command I run is identical.
processBuilder = new ProcessBuilder( "net",
"use",
"T:",
"\\\\" + SERVER_NAME + "\\" + SHARE_NAME + "\\" + FOLDER,
"/USER:" + USERNAME,
PASSWORD,
"/PERSISTENT:NO"
);
The really strange thing is that when I open the file browser that's built into my applications gui for the first time, it shows the drive with the correct label, even though windows file explorer shows the old label. This is only the case when you open it for the first time though and then it will not show any new drive labels.
I'm guessing this is more of a windows specific thing that I'm dealing with and not an issue with the code. Any ideas how I can force windows to refresh the label or any other ideas?
Update - I discovered that if I restart explorer.exe
process in task manager it will refresh the label when I reopen the file explorer. I don't want to do this in my application because it refreshes the entire windows desktop interface but it might provide some insight.
Update 2 - I also noticed that if I Thread.sleep()
for 5 seconds between unmounting the initial drive and mounting the selected drive the label updates correctly... This does the job but it's annoying that this is the best solution I've found so far. I'm currently figuring out the minimum amount of time I need to sleep the thread for... 1 second was not long enough.
New-PSDrive
waitFor()
on theProcess
object?waitFor()
or not.