C#学习教程:File.Move和File.Copy给出IOException登录失败分享


File.Move和File.Copy给出IOException登录失败

我正在为C#中的fujitsu扫描器编写一个加载项。 文档被扫描到本地目录,我想将它们复制到网络上的pc。 我一直收到System.IO.IOException:登录失败:虽然未知用户名或密码错误。 我尝试将它们复制到公共目录(至少我认为是公共目录),我仍然得到相同的结果。

对于我可以尝试的东西的任何想法? 我认为这里的代码不是问题,但无论如何它都在这里。

private bool moveTheFile(String source, String destination) { System.DirectoryServices.DirectoryEntry dntry = null; try { //System.IO.File.Move(source, destination); System.IO.File.Copy(source, destination); System.IO.File.Delete(source); if (System.IO.File.Exists(destination)) { return true; } else { return false; } } catch (Exception err) { _host.WriteSystemLog(LogType.Information, "E1000099", "File.Move Error " + err.ToString()); return false; } } 

您的问题是由于机器正在使用LOCAL SERVICE帐户或甚至无法访问共享公用文件夹的内容。 您需要冒充我相信的其他帐户。 我在不同的站点上找到以下代码来执行此操作:

 public WindowsImpersonationContext ImpersonateUser(string sUsername, string sDomain, string sPassword) { // initialize tokens IntPtr pExistingTokenHandle = new IntPtr(0); IntPtr pDuplicateTokenHandle = new IntPtr(0); pExistingTokenHandle = IntPtr.Zero; pDuplicateTokenHandle = IntPtr.Zero; // if domain name was blank, assume local machine if (sDomain == "") sDomain = System.Environment.MachineName; try { string sResult = null; const int LOGON32_PROVIDER_DEFAULT = 0; // create token const int LOGON32_LOGON_INTERACTIVE = 2; //const int SecurityImpersonation = 2; // get handle to token bool bImpersonated = LogonUser(sUsername, sDomain, sPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle); // did impersonation fail? if (false == bImpersonated) { int nErrorCode = Marshal.GetLastWin32Error(); sResult = "LogonUser() failed with error code: " + nErrorCode + "rn"; // show the reason why LogonUser failed MessageBox.Show(this, sResult, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } // Get identity before impersonation sResult += "Before impersonation: " + WindowsIdentity.GetCurrent().Name + "rn"; bool bRetVal = DuplicateToken(pExistingTokenHandle, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref pDuplicateTokenHandle); // did DuplicateToken fail? if (false == bRetVal) { int nErrorCode = Marshal.GetLastWin32Error(); // close existing handle CloseHandle(pExistingTokenHandle); sResult += "DuplicateToken() failed with error code: " + nErrorCode + "rn"; // show the reason why DuplicateToken failed MessageBox.Show(this, sResult, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); return null; } else { // create new identity using new primary token WindowsIdentity newId = new WindowsIdentity (pDuplicateTokenHandle); WindowsImpersonationContext impersonatedUser = newId.Impersonate(); // check the identity after impersonation sResult += "After impersonation: " + WindowsIdentity.GetCurrent().Name + "rn"; MessageBox.Show(this, sResult, "Success", MessageBoxButtons.OK, MessageBoxIcon.Information); return impersonatedUser; } } catch (Exception ex) { throw ex; } finally { // close handle(s) if (pExistingTokenHandle != IntPtr.Zero) CloseHandle(pExistingTokenHandle); if (pDuplicateTokenHandle != IntPtr.Zero) CloseHandle(pDuplicateTokenHandle); } } 

以下是支持方法:

上述就是C#学习教程:File.Move和File.Copy给出IOException登录失败分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—猴子技术宅(www.ssfiction.com)

 [DllImport("advapi32.dll", SetLastError=true)] public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken); // closes open handes returned by LogonUser [DllImport("kernel32.dll", CharSet=CharSet.Auto)] public extern static bool CloseHandle(IntPtr handle); // creates duplicate token handle [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)] public extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle); // group type enum public enum SECURITY_IMPERSONATION_LEVEL : int { SecurityAnonymous = 0, SecurityIdentification = 1, SecurityImpersonation = 2, SecurityDelegation = 3 } 

本文来自网络收集,不代表猴子技术宅立场,如涉及侵权请点击右边联系管理员删除。

如若转载,请注明出处:https://www.ssfiction.com/ckf/1269781.html

(0)
上一篇 1天前
下一篇 1天前

精彩推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注