映射网络驱动器Windows服务

时间:2018-06-11 17:02:30

标签: c# windows service windows-services network-drive

我使用c#创建一个Windows服务,在网络驱动器上找到新的zip文件并将它们解压缩到本地目录。我使用此Map Network Drive API进行映射。我首先调用map函数,如果失败,我取消映射驱动器并尝试重新映射驱动器。 错误:

  • mapDrive()"本地设备名称已被使用" ,然后尝试
  • unmapDrive()"此网络连接不存在"
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;
    }
}

他们被称为

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();

    }
}

0 个答案:

没有答案