I'm using c# to create a windows service that finds new zip files on a network drive and unzips them to a local directory. I'm using this Map Network Drive API to do the mapping. I first call the map function and if that fails I unmap the drive and attempt to remap the drive again. Errors:
- mapDrive() "the local device name is already in use" , then try
- unmapDrive() "This network connection does not exist"
private bool unmapDrive()
{
try
{
NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
oNetDrive.LocalDrive = driveLetter;
oNetDrive.Force = true;
oNetDrive.ShareName = @"\\IPADDRESS\LogFiles";
oNetDrive.UnMapDrive();
return true;
}
catch(Exception e)
{
Log(e.Message);
return false;
}
}
private bool mapDrive()
{
try
{
NetworkDrive oNetDrive = new aejw.Network.NetworkDrive();
oNetDrive.LocalDrive = driveLetter;
//oNetDrive.SaveCredentials = true;
oNetDrive.ShareName = @"\\IPADDRESS\LogFiles";
oNetDrive.PromptForCredentials = true;
oNetDrive.MapDrive();
return true;
}
catch(Exception e)
{
Log(e.Message);
return false;
}
}
they are called like so
if (!mapDrive())
{
unmapDrive();
if (!mapDrive())
{
//if drive cannot be mapped, program will not work
Log("Discoverer has failed to map drive, shutting down");
this.Stop();
}
}