Thursday, May 31, 2007

How to stop a network drive disconnecting

We have a problem in our office where network drives disconnect due to inactivity but don't reconnect silently as they should. Instead they say:
An error occurred while reconnecting L: to \\server\share.
The local device name is already in use. This connection has not been restored.
Our server is OS X Tiger (i.e. Samba), and the problem only occurs on machines that are members of the domain, but where the users log in locally.

Changing deadtime in /etc/smb.conf didn't help.

I have discovered that I can reliably stop the problem occurring if I keep a file open on the server. I guess this stops the automatic disconnection, which avoids the reconnection problem. Microsoft Word will do this for you fine, but it's annoying to have that application running all the time so I've created a simple program that will lock a file for you.

To use it, download DriveLocker.exe and shove it on your network drive. Then add a shortcut to it in your Startup group in Programs. The shortcut must include the name of a file to create and lock as its parameter. e.g. the Target field in the shortcut might read:

"L:\DriveLocker.exe" L:\.DriveLocker_OCB

More on C# with Office

Sometimes the Excel or Word API provides more than one way to call a function, but only one function is visible in C#. In this case you can use InvokeMember to call the version you want. e.g.

Axes axes = (Axes)chart.GetType().InvokeMember("Axes", BindingFlags.InvokeMethod, null, chart, no_args);