5

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.

16
  • Have you tried clicking "Refresh" in your Windows Explorer (or press "F5")?
    – knittl
    Aug 8 at 14:57
  • @knittl Yes, refreshing or even closing and reopening file explorer doesn't show the new drive label
    – rhowell
    Aug 8 at 14:58
  • See if you get on any better with Powershell New-PSDrive
    – g00se
    Aug 8 at 15:38
  • When you execute the unmount command, do you wait for it to complete before remounting? In other words, do you call waitFor() on the Process object?
    – Olivier
    Aug 11 at 19:02
  • 1
    @D.Kovács Note that the OP still didn't say if they were calling waitFor() or not.
    – Olivier
    Aug 15 at 7:32

2 Answers 2

1

Windows keeps the mapping of label to network locations for the current user in the registry, cf. this SuperUser post.

Apparently—based on the discussion in comments on the question—there is some kind of interaction between the spawned process being stopped and the registry update not finishing fully. I.e., the process creates the mapping, which triggers an update in the registry, but if the process terminates before the registry update is finished, the registry update is cancelled (or maybe not even invoked? It may be that in order it to be invoked, the process handle, which created the mapping still has to be there for the internal OS trigger).

Thus, the "proper" solution is not a waitFor() (as it does not work), or a sleep(n) (because there is no realiable way to tell for any n that it will work, because of OS scheduling). The "proper" solution is to keep the process alive and check whether the registry update happens (i.e., the label is registered and available) and when it has been finished, then you can close the process.

Of course, pragmatically speaking, a sleep(10_000); should do the trick, you are just wasting some negligible amount of CPU cycles and RAM.

0

I have two methods that could work.

  1. Simulate F5 Key Press Event: Just simulate the F5 Key Press event in Java. It should target the File Explorer windows.

Sample Code:

import java.awt.Robot;
import java.awt.event.KeyEvent;

public class RefreshExplorer {
    public static void main(String[] args) {
        try {
            Robot robot = new Robot();
            robot.keyPress(KeyEvent.VK_F5);
            robot.keyRelease(KeyEvent.VK_F5);
        } catch (Exception e) {
            System.out.println(e);
            e.printStackTrace();
        }
    }
}
  1. Optimize the sleep method with a loop: By using a loop in Java that checks lets say every 500 ms if the job is done, you don't need to wait 5 seconds and you can find out the fastest time that works by trying shorter times.

In the code the method isDriveLabelUpdated() would be a method you need to code that checks if the drive label was updated or not and returns a boolean.

Sample Code:

for (int i = 0; i < 10; i++) { 
    Thread.sleep(500);  // Sleep for 500 milliseconds
    if (isDriveLabelUpdated()) { 
        break;  // Exit the loop if the update is detected
    }
}

If you need help for the second idea or you have any questions feel free to ask.

1
  • Refreshing file explorer does not show the updated label unfortunately. There was some discussion on this in the comments of the original post.
    – rhowell
    Aug 18 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.