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

问题描述 投票:0回答:2

我正在用 C# 为富士通扫描仪编写一个插件。文档被扫描到本地目录,我想将它们复制到网络上的电脑上。我不断收到 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;
        }
    }
c# file networking copy ioexception
2个回答
2
投票

您的问题是由于计算机正在使用本地服务帐户或什至无法访问共享公共文件夹的帐户。我认为您需要冒充另一个帐户。我在另一个网站上找到了以下代码来执行此操作:

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 + "\r\n";
            // 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 + "\r\n";
        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 + "\r\n";
            // 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 + "\r\n";

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

支持方法如下:

[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
    }

0
投票

我遇到了同样的问题,就我而言,这是由于网络链接造成的。我没有使用 \192.168.1.x,而是将其更改为共享名,例如 \pc2 old。

© www.soinside.com 2019 - 2024. All rights reserved.