File.Move和File.Copy给IOException登录失败文档被扫描到本地目录,我想将它们复制到网络上的一台电脑上。我不断收到System.IO.IOException:Loginfailed:unknownusernameorbadpassword虽然。我尝试将它们复制到公共目录(至少我认为是),但我仍然得到相同的结果。有什么我可以尝试的想法吗?我不认为这里的代码是问题所在,但无论如何它都在这里。privateboolmoveTheFile(Stringsource,Stringdestination){System.DirectoryServices.DirectoryEntrydntry=null;尝试{//System.IO.File.Move(源,目标);System.IO.File.Copy(源,目标);System.IO.File.Delete(来源);如果(System.IO.File.Exists(目的地)){返回真;}else{返回错误;}}catch(Exceptionerr){_host.WriteSystemLog(LogType.Information,"E1000099","File.MoveError"+err.ToString());返回假;}}你的问题是由于机器正在使用LOCALSERVICE帐户或什至无法访问共享公用文件夹的内容。我相信您需要冒充另一个帐户。我在不同的站点上找到以下代码来执行此操作:publicWindowsImpersonationContextImpersonateUser(stringsUsername,stringsDomain,stringsPassword){//初始化令牌IntPtrpExistingTokenHandle=newIntPtr(0);IntPtrpDuplicateTokenHandle=newIntPtr(0);pExistingTokenHandle=IntPtr.Zero;pDuplicateTokenHandle=IntPtr.Zero;//如果域名为空,假设为本地机器if(sDomain=="")sDomain=System.Environment.MachineName;尝试{字符串sResult=null;constintLOGON32_PROVIDER_DEFAULT=0;//创建令牌constintLOGON32_LOGON_INTERACTIVE=2;//constintSecurityImpersonation=2;//获取tokenboolbImpersonated=LogonUser(sUsername,sDomain,sPassword,LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,refpExistingTokenHandle);//模拟失败了吗?如果(false==bImpersonated){intnErrorCode=Marshal.GetLastWin32Error();sResult="LogonUser()失败,错误代码:"+nErrorCode+"rn";//显示LogonUser的原因失败MessageBox.Show(this,sResult,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);}//获取模拟前的身份sResult+="模拟前:"+WindowsIdentity.GetCurrent().Name+"rn";boolbRetVal=DuplicateToken(pExistingTokenHandle,(int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation,refpDuplicateTokenHandle);//DuplicateToken失败了吗?如果(false==bRetVal){intnErrorCode=Marshal.GetLastWin32Error();//关闭现有句柄CloseHandle(pExistingTokenHandle);sResult+="DuplicateToken()失败,错误代码:"+nErrorCode+"rn";//显示DuplicateToken失败的原因MessageBox.Show(this,sResult,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);返回空值;}else{//使用新的主令牌创建新身份WindowsIdentitynewId=newWindowsIdentity(pDuplicateTokenHandle);WindowsImpersonationContextimpersonatedUser=newId.Impersonate();//模拟后检查身份sResult+="模拟后:"+WindowsIdentity.GetCurrent().Name+"rn";MessageBox.Show(this,sResult,"成功",MessageBoxButtons.OK,MessageBoxIcon.Information);返回模拟用户;}}catch(Exceptionex){抛出ex;}finally{//关闭句柄if(pExistingTokenHandle!=IntPtr.Zero)CloseHandle(pExistingTokenHandle);如果(pDuplicateTokenHandle!=IntPtr.Zero)CloseHandle(pDuplicateTokenHandle);以下是支持的方法:以上是C#学习教程:File.Move和File.Copy给出了IOException登录失败分享的所有内容。如果对大家有用,需要进一步了解C#学习教程,希望大家多加关注——,字符串pszPassword,intdwLogonType,intdwLogonProvider,refIntPtrphToken);//关闭LogonUser[DllImport("kernel32.dll",CharSet=CharSet.Auto)]publicexternstaticboolCloseHandle(IntPtrhandle);//创建重复的令牌句柄[DllImport("advapi32.dll",CharSet=CharSet.Auto,SetLastError=true)]publicexternstaticboolDuplicateToken(IntPtrExistingTokenHandle,intSECURITY_IMPERSONATION_LEVEL,refIntPtrDuplicateTokenHandle);//grouptypeenumpublicenumSECURITY_IMPERSONATION_LEVEL:int{SecurityAnonymous=0,SecurityIdentification=1,SecurityImpersonation=2,SecurityDelegation=3}本文收集自网络,不代表立场。如涉及侵权,请点击右侧联系管理员删除。如有转载请注明出处:
