'How can I obtain access to a networked location?
My program, when started up with the system, is unable to access a networked location:
fn main() {
ensure_network("\\\\SERVER\\".to_string());
}
fn ensure_network(network_dir: String) {
let timer = std::time::Instant::now();
let mut prev_counter = 0;
loop {
if std::fs::read_dir(&network_dir).is_ok() {
break;
}
if timer.elapsed().as_secs() > prev_counter + 60 {
println!("Still Failing.");
prev_counter = timer.elapsed().as_secs();
}
std::hint::spin_loop();
}
println!("Network access obtained (Time elapsed: {})",timer.elapsed().as_secs_f32());
}
This program should start and wait for the network to be connected. But, it never finds the networked location. However, if I start another instance of this application it will work if the connection is already established. Even further, the second instance appears to get the original instance to work as well. The reason the first instance doesn't have an established connection is because it starts up with the PC.
I have tried adding pauses to the beginning and as you can see it has a loop that should re-check. But, this program fails over and over. I have logged an hour of trying with no success. The network itself automatically connects. The way I "establish" the connection before running a second instance is simply by accessing files on the network from file-explorer or direct links on my desktop. I have even tried launching net use
and it simply fails to reach the location.
Edit: The second instance does not always cause the first to start working. It is inconsistent.
Edit 2: This program is started using Task Scheduler and the option 'Run with highest privileges.' I have found that if the program is failing, manually starting any application 'as administrator' will cause this application to begin working. I have also found that disabling 'run with highest privileges' also causes the program to work. The problem with that, however, is that the program then no longer has the permissions needed to access files.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|