4

I have a C# application which loads at startup, and logs data to a network drive, which is mounted as X:

When the machine first boots, the application throws an error that X:\ is not available. If I restart the app, same error.

However, if I open Windows Explorer and double click to browse the drive, I can then run the application and it will connect to X: just fine.

Do network drives not automatically initialise on startup, despite being mapped? Is there a way to set them to initialise automatically?

Gavin Coates
  • 1,385
  • 1
  • 17
  • 40
  • Based on network connection it takes some time for them to get mapped and connect when you boot your system. You may need to increase time or add functionality to wait and check if network is mapped and throw error only if it crosses a set threshold or time. – rs. Apr 05 '12 at 13:55
  • @rs: I have tried waiting over 10 minutes, and it doesn't ever seem to map them. – Gavin Coates Apr 05 '12 at 14:16

2 Answers2

3

Ive had the exact same issue. I don't know if there are better methods out there, but I added this to my code before accessing the mapped drive.

Process mapDrive = new Process();
mapDrive.StartInfo.FileName = "net.exe";
mapDrive.StartInfo.Arguments = @"use c: \\server\share";
mapDrive.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
mapDrive.Start();

That way whether the drive is available at start up or not it will always be available.

JimDel
  • 4,239
  • 10
  • 47
  • 96
  • this works, however the path to the directory needs to be entered in a config file, which is then read in on startup. It may be on a network drive, or on a local drive. I would need to check if it is local or network before trying to mount this. It's a bit messy. Is there no way in windows to just automatically mount the drive? – Gavin Coates Apr 05 '12 at 14:43
  • 1
    "Is there no way in windows to just automatically mount the drive?"... If there is, I want to know it too. – JimDel Apr 05 '12 at 15:00
  • I decided to just do it this way, as I can't find any other option. You may need to disconnect the drive first though, by using `StartInfo.Arguments = @"use x: /d";`. Also, you may wish to set windows to not auto disconnect the network drive when idle - `net config server /autodisconnect:-1` – Gavin Coates Apr 10 '12 at 14:36
0

See How Can I Access a Mapped Network Drive here on SO for a list of things to check. My guess is either the drive does not exist for your user, or there is a permissions issue accessing it. Another item to check is the order in which you call Impersonate, assuming that you are doing so, that is.

According to Cannot Access Files On Mapped Drive From Windows Service you should not do this at all. See the Microsoft link(s) provided in the accepted answer.

Community
  • 1
  • 1
Joshua Drake
  • 2,581
  • 2
  • 34
  • 53